How to query VRAM usage for a specific MIG UUID

I am looking to detemine how much VRAM is used for a specific MIG UUID. I came up with a convoluted solution but I feel like there must be a better way. Am I missing something obvious?

nvidia-smi doesn’t seem to support this using --query-gpu or similar, so I used dcgmi dmon. dcgmi dmon entity selectors don’t seem to include MIG UUIDs. Based on the output of nvidia-smi -L and dcgmi discovery -c, it looks like dcgmi’s “instance number” is just the nvidia-smi"device number" + 1, and the GPU numbers are the same.

$ nvidia-smi -L
GPU 0: NVIDIA RTX PRO 6000 Blackwell Server Edition (UUID: GPU-4b88bbd5-409f-a58a-67da-cd28a6fc9b80)
  MIG 2g.48gb     Device  0: (UUID: MIG-548e2364-a769-5eaa-9044-2ca2b946569a)
  MIG 2g.48gb     Device  1: (UUID: MIG-cdd572b4-599d-5fe5-9c7a-1ea86eb15b48)
$ dcgmi discovery -c
+-------------------+--------------------------------------------------------------------+
| Instance Hierarchy                                                                     |
+===================+====================================================================+
| GPU 0             | GPU GPU-4b88bbd5-409f-a58a-67da-cd28a6fc9b80 (EntityID: 0)         |
| -> I 0/1          | GPU Instance (EntityID: 0)                                         |
|    -> CI 0/1/0    | Compute Instance (EntityID: 0)                                     |
| -> I 0/2          | GPU Instance (EntityID: 1)                                         |
|    -> CI 0/2/0    | Compute Instance (EntityID: 1)                                     |
+-------------------+--------------------------------------------------------------------+

This is a pretty simple MIG partition scheme (half & half), I am not super confident that this plus-one trick is valid for any partition scheme. Assuming this plus-one trick is valid, I came up with the following shell script to get VRAM usage using the MIG UUID provided to me by Slurm in $CUDA_VISIBLE_DEVICES:

IFS=',' read -ra MIG_UUIDs <<< "$CUDA_VISIBLE_DEVICES"
for MIG_UUID in "${MIG_UUIDs[@]}"; do
    # `nvidia-smi -L` doesn't seem to support any machine-readable output format
    # filter for lines that come before this mig, then get the last GPU number
    mig_nvsmi_gpu_number="$(env -i nvidia-smi -L | grep -B 99999 "$MIG_UUID" | grep '^GPU' | tail -n 1 | sed -E 's/^GPU ([0-9]+).*/\1/')"
    mig_nvsmi_device_number="$(env -i nvidia-smi -L | grep "$MIG_UUID" | sed -E 's/.*Device[ ]+([0-9]+).*/\1/')"
    mig_dcgmi_instance_id="$mig_nvsmi_gpu_number/$(( mig_nvsmi_device_number + 1))"
    framebuffer_used=252 # `dcgmi dmon --list`
    # `dcgmi dmon` doesn't seem to support any machine-readable output format
    # output example:
    # #Entity    FBUSD
    # ID
    # GPU-I 8    127
    VRAM_USED="$(dcgmi dmon --count 1 --entity-id "$mig_dcgmi_instance_id" --field-id "$framebuffer_used" | tail -n 1 | awk '{print $3}')"
done