NVIDIA Multi-Instance GPU (MIG) allows partitioning a single GPU into multiple isolated instances. Given a GPU's total compute units and memory, and a set of requested MIG profiles, determine if the allocation is feasible and compute the resource utilization.
def mig_allocation(
total_sms: int,
total_memory_gb: float,
profiles: list[dict]
) -> dict:
# Each profile: {"name": str, "sms": int, "memory_gb": float, "count": int}
total_sms_requested = sum(p["sms"] * p["count"] for p in profiles)
total_mem_requested = sum(p["memory_gb"] * p["count"] for p in profiles)
feasible = total_sms_requested <= total_sms and total_mem_requested <= total_memory_gb
instances = []
for p in profiles:
for i in range(p["count"]):
instances.append({
"name": f"{p['name']}-{i}",
"sms": p["sms"],
"memory_gb": p["memory_gb"]
})
sm_utilization = total_sms_requested / total_sms * 100 if total_sms > 0 else 0
mem_utilization = total_mem_requested / total_memory_gb * 100 if total_memory_gb > 0 else 0
return {
"feasible": feasible,
"instances": instances,
"total_instances": len(instances),
"sm_utilization_pct": round(sm_utilization, 2),
"memory_utilization_pct": round(mem_utilization, 2),
"sms_remaining": total_sms - total_sms_requested,
"memory_remaining_gb": round(total_memory_gb - total_mem_requested, 2)
}