Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SD-113] Measure power consumption related to model in memory #52

20 changes: 19 additions & 1 deletion jetson/power_logging/model/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class BenchmarkMetrics(BaseModel):
latencies: list[float] # in seconds
avg_latency: float # in seconds
avg_throughput: float
warmup: tuple[str, str]
model_load: tuple[str, str]
pt_model_load_end: str


def load_model(model_name: str) -> Any:
Expand Down Expand Up @@ -74,6 +77,7 @@ def benchmark(args: argparse.Namespace) -> None:
input_data = torch.randn(args.input_shape, device=DEVICE)
model = load_model(args.model)
model.eval().to(DEVICE)
pt_model_load_end = datetime.now().strftime("%Y%m%d-%H%M%S")

dtype = torch.float32
if args.dtype == "float16":
Expand All @@ -85,6 +89,7 @@ def benchmark(args: argparse.Namespace) -> None:
model = model.to(dtype)
print(f"Using {DEVICE=} for benchmarking")

model_load_start = datetime.now().strftime("%Y%m%d-%H%M%S")
exp_program = torch.export.export(model, tuple([input_data]))
model = torch_tensorrt.dynamo.compile(
exported_program=exp_program,
Expand All @@ -99,20 +104,30 @@ def benchmark(args: argparse.Namespace) -> None:
# Setting it to True returns PythonTorchTensorRTModule which has different profiling approach
use_python_runtime=True,
)
model_load_end = datetime.now().strftime("%Y%m%d-%H%M%S")

print("Sleeping for 10 seconds...")
time.sleep(10)

st = time.perf_counter()
print("Warm up ...")
warmup_start = datetime.now().strftime("%Y%m%d-%H%M%S")
with torch.no_grad():
for _ in range(args.warmup):
_ = model(input_data)
warmup_end = datetime.now().strftime("%Y%m%d-%H%M%S")
print(f"Warm complete in {time.perf_counter() - st:.2f} sec ...")

print("Sleeping for 10 seconds...")
time.sleep(10)

print("Start timing using tensorrt backend ...")
torch.cuda.synchronize()
# Recorded in milliseconds
start_events = [torch.cuda.Event(enable_timing=True) for _ in range(args.runs)]
end_events = [torch.cuda.Event(enable_timing=True) for _ in range(args.runs)]

print("Starting inference...")
with torch.no_grad():
for i in tqdm(range(args.runs)):
# Hack for enabling profiling
Expand All @@ -129,7 +144,7 @@ def benchmark(args: argparse.Namespace) -> None:
_ = model(input_data)
end_events[i].record()

print("Sleeping for 1 second(s)...")
# print("Sleeping for 1 second(s)...")
time.sleep(1)

end.record()
Expand All @@ -153,6 +168,9 @@ def benchmark(args: argparse.Namespace) -> None:
latencies=timings, # in seconds
avg_throughput=avg_throughput,
avg_latency=np.mean(timings), # in seconds
warmup=(warmup_start, warmup_end),
model_load=(model_load_start, model_load_end),
pt_model_load_end=pt_model_load_end,
)

model_dir = f"{args.result_dir}/{args.model}"
Expand Down
1 change: 1 addition & 0 deletions jetson/power_logging/power_research/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Init for power research."""
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"from pathlib import Path\n",
"import json\n",
"import matplotlib.pyplot as plt\n",
"import sys; sys.path.insert(0, '..')\n",
"\n",
"from utils import parse_latencies, parse_power_log"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"from pathlib import Path\n",
"import json\n",
"import matplotlib.pyplot as plt\n",
"import sys; sys.path.insert(0, '..')\n",
"\n",
"from utils import parse_latencies, parse_power_log"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"from pathlib import Path\n",
"import json\n",
"import matplotlib.pyplot as plt\n",
"import sys; sys.path.insert(0, '..')\n",
"\n",
"from utils import parse_latencies, parse_power_log"
]
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ def parse_latencies(
return latency_data


def get_power_index(power_data, timestamp):
index = 0
while True:
power_timestamp = power_data[index][0]
if power_timestamp > timestamp:
return index
index += 1


def plot_inference_cycle_power(
cycle: int, power_data: list[tuple], latency_data: dict[list[tuple]], delay: int
) -> None:
Expand Down
1 change: 1 addition & 0 deletions jetson/power_logging/raw_data/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/prebuilt_models
/power_profile_between_inference
/power_profile_model_loading
6 changes: 6 additions & 0 deletions jetson/power_logging/raw_data/power_profile_model_loading.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
outs:
- md5: e4ea48d9fa9ba9a249f750e3032ab84e.dir
size: 531338892
nfiles: 21
hash: md5
path: power_profile_model_loading
4 changes: 2 additions & 2 deletions jetson/power_logging/run_experiment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -eou pipefail
IDLE_DURATION=120

# Directory to store results
RESULT_DIR="raw_data/power_profile_between_inference"
RESULT_DIR="raw_data/power_profile_model_loading"

echo "Running idling power measurement..."
python measure_idling_power.py \
Expand All @@ -25,7 +25,7 @@ sleep 120
# models=("alexnet" "vgg11" "vgg13" "vgg16" "vgg19" "mobilenet_v2" "mobilenet_v3_small" "mobilenet_v3_large" "resnet18" "resnet34" "resnet50" "resnet101" "resnet152" "lenet" "resnext50_32x4d" "resnext101_32x8d" "resnext101_64x4d" "convnext_tiny" "convnext_small" "convnext_base")
# # Number of inference cycles
# RUNS=30000
models=("alexnet" "vgg16" "vgg19")
models=("resnet18" "resnet34" "alexnet" "vgg16" "vgg19")
RUNS=3000

# Iterate through models and run measure_inference_power.py script
Expand Down