Unverified Commit 5637cc9a authored by Ruilong Li(李瑞龙)'s avatar Ruilong Li(李瑞龙) Committed by GitHub
Browse files

Optimize examples for better performance (#59)

* zeros -> emtpy in cuda

* disable cuda hint if is been built

* update examples with better perf

* bump version to 0.2.0

* update readme

* update index and readm

* clean up doc
parent 3d958321
......@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "nerfacc"
version = "0.1.8"
version = "0.2.0"
description = "A General NeRF Acceleration Toolbox."
readme = "README.md"
authors = [{name = "Ruilong", email = "ruilongli94@gmail.com"}]
......
from typing import Callable
import torch
import nerfacc
class Profiler:
def __init__(self, warmup=10, repeat=1000):
self.warmup = warmup
self.repeat = repeat
def __call__(self, func: Callable):
# warmup
for _ in range(self.warmup):
func()
torch.cuda.synchronize()
# profile
with torch.profiler.profile(
activities=[
torch.profiler.ProfilerActivity.CPU,
torch.profiler.ProfilerActivity.CUDA,
],
profile_memory=True,
) as prof:
for _ in range(self.repeat):
func()
torch.cuda.synchronize()
# return
events = prof.key_averages()
self_cpu_time_total = (
sum([event.self_cpu_time_total for event in events]) / self.repeat
)
self_cuda_time_total = (
sum([event.self_cuda_time_total for event in events]) / self.repeat
)
self_cuda_memory_usage = max(
[event.self_cuda_memory_usage for event in events]
)
return (
self_cpu_time_total, # in us
self_cuda_time_total, # in us
self_cuda_memory_usage, # in bytes
)
def main():
device = "cuda:0"
torch.manual_seed(42)
profiler = Profiler(warmup=10, repeat=1000)
# contract
print("* contract")
x = torch.rand([1024, 3], device=device)
roi = torch.tensor([0, 0, 0, 1, 1, 1], dtype=torch.float32, device=device)
fn = lambda: nerfacc.contract(
x, roi=roi, type=nerfacc.ContractionType.UN_BOUNDED_TANH
)
cpu_t, cuda_t, cuda_bytes = profiler(fn)
print(f"{cpu_t:.2f} us, {cuda_t:.2f} us, {cuda_bytes / 1024 / 1024:.2f} MB")
if __name__ == "__main__":
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment