Skip to content

Commit 90d30bc

Browse files
committed
Add homegrown vector lib, log callbacks instead of iostream, misc refactor
1 parent 8a54281 commit 90d30bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3446
-1584
lines changed

CMakeLists.txt

+2-13
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,7 @@ function(TCNN_AUTODETECT_CUDA_ARCHITECTURES OUT_VARIABLE)
105105
"}\n"
106106
)
107107

108-
if (CMAKE_CUDA_COMPILER_LOADED) # CUDA as a language
109-
try_run(run_result compile_result ${PROJECT_BINARY_DIR} ${file} RUN_OUTPUT_VARIABLE compute_capabilities)
110-
else()
111-
try_run(
112-
run_result compile_result ${PROJECT_BINARY_DIR} ${file}
113-
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${CUDA_INCLUDE_DIRS}"
114-
LINK_LIBRARIES ${CUDA_LIBRARIES}
115-
RUN_OUTPUT_VARIABLE compute_capabilities
116-
)
117-
endif()
118-
108+
try_run(run_result compile_result ${PROJECT_BINARY_DIR} ${file} RUN_OUTPUT_VARIABLE compute_capabilities)
119109
if (run_result EQUAL 0)
120110
# If the user has multiple GPUs with the same compute capability installed, list that capability only once.
121111
list(REMOVE_DUPLICATES compute_capabilities)
@@ -257,8 +247,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_BINARY_DIR})
257247
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR})
258248

259249
set(TCNN_SOURCES
260-
src/common.cu
261-
src/common_device.cu
250+
src/common_host.cu
262251
src/cpp_api.cu
263252
src/cutlass_mlp.cu
264253
src/encoding.cu

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ using namespace tcnn;
4545

4646
auto model = create_from_config(n_input_dims, n_output_dims, config);
4747

48-
// Train the model (batch_size must be a multiple of tcnn::batch_size_granularity)
48+
// Train the model (batch_size must be a multiple of tcnn::BATCH_SIZE_GRANULARITY)
4949
GPUMatrix<float> training_batch_inputs(n_input_dims, batch_size);
5050
GPUMatrix<float> training_batch_targets(n_output_dims, batch_size);
5151

bindings/torch/setup.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
from setuptools import setup
55
from pkg_resources import parse_version
66
import subprocess
7+
import shutil
78
import sys
89
import torch
10+
from glob import glob
911
from torch.utils.cpp_extension import BuildExtension, CUDAExtension
1012

1113
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
@@ -123,15 +125,21 @@ def find_cl_path():
123125
# List of sources.
124126
bindings_dir = os.path.dirname(__file__)
125127
root_dir = os.path.abspath(os.path.join(bindings_dir, "../.."))
126-
base_definitions = []
128+
129+
base_definitions = [
130+
# PyTorch-supplied parameters may be unaligned. TCNN must be made aware of this such that
131+
# it does not optimize for aligned memory accesses.
132+
"-DTCNN_PARAMS_UNALIGNED",
133+
]
134+
127135
base_source_files = [
128136
"tinycudann/bindings.cpp",
129137
"../../dependencies/fmt/src/format.cc",
130138
"../../dependencies/fmt/src/os.cc",
131139
"../../src/cpp_api.cu",
132-
"../../src/common.cu",
133-
"../../src/common_device.cu",
140+
"../../src/common_host.cu",
134141
"../../src/encoding.cu",
142+
"../../src/object.cu",
135143
]
136144

137145
if include_networks:

bindings/torch/tinycudann/bindings.cpp

+29-38
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <json/json.hpp>
4545

4646
#include <pybind11_json/pybind11_json.hpp>
47+
#include <pybind11/functional.h>
4748

4849
#include <tiny-cuda-nn/cpp_api.h>
4950

@@ -53,10 +54,10 @@
5354
#define CHECK_THROW(x) \
5455
do { if (!(x)) throw std::runtime_error(std::string(FILE_LINE " check failed " #x)); } while(0)
5556

56-
c10::ScalarType torch_type(tcnn::cpp::EPrecision precision) {
57+
c10::ScalarType torch_type(tcnn::cpp::Precision precision) {
5758
switch (precision) {
58-
case tcnn::cpp::EPrecision::Fp32: return torch::kFloat32;
59-
case tcnn::cpp::EPrecision::Fp16: return torch::kHalf;
59+
case tcnn::cpp::Precision::Fp32: return torch::kFloat32;
60+
case tcnn::cpp::Precision::Fp16: return torch::kHalf;
6061
default: throw std::runtime_error{"Unknown precision tcnn->torch"};
6162
}
6263
}
@@ -246,41 +247,19 @@ class Module {
246247
return output;
247248
}
248249

249-
uint32_t n_input_dims() const {
250-
return m_module->n_input_dims();
251-
}
250+
uint32_t n_input_dims() const { return m_module->n_input_dims(); }
252251

253-
uint32_t n_params() const {
254-
return (uint32_t)m_module->n_params();
255-
}
252+
uint32_t n_params() const { return (uint32_t)m_module->n_params(); }
253+
tcnn::cpp::Precision param_precision() const { return m_module->param_precision(); }
254+
c10::ScalarType c10_param_precision() const { return torch_type(param_precision()); }
256255

257-
tcnn::cpp::EPrecision param_precision() const {
258-
return m_module->param_precision();
259-
}
256+
uint32_t n_output_dims() const { return m_module->n_output_dims(); }
257+
tcnn::cpp::Precision output_precision() const { return m_module->output_precision(); }
258+
c10::ScalarType c10_output_precision() const { return torch_type(output_precision()); }
260259

261-
c10::ScalarType c10_param_precision() const {
262-
return torch_type(param_precision());
263-
}
260+
nlohmann::json hyperparams() const { return m_module->hyperparams(); }
261+
std::string name() const { return m_module->name(); }
264262

265-
uint32_t n_output_dims() const {
266-
return m_module->n_output_dims();
267-
}
268-
269-
tcnn::cpp::EPrecision output_precision() const {
270-
return m_module->output_precision();
271-
}
272-
273-
c10::ScalarType c10_output_precision() const {
274-
return torch_type(output_precision());
275-
}
276-
277-
nlohmann::json hyperparams() const {
278-
return m_module->hyperparams();
279-
}
280-
281-
std::string name() const {
282-
return m_module->name();
283-
}
284263

285264
private:
286265
std::unique_ptr<tcnn::cpp::Module> m_module;
@@ -296,22 +275,34 @@ Module create_network(uint32_t n_input_dims, uint32_t n_output_dims, const nlohm
296275
}
297276
#endif
298277

299-
Module create_encoding(uint32_t n_input_dims, const nlohmann::json& encoding, tcnn::cpp::EPrecision requested_precision) {
278+
Module create_encoding(uint32_t n_input_dims, const nlohmann::json& encoding, tcnn::cpp::Precision requested_precision) {
300279
return Module{tcnn::cpp::create_encoding(n_input_dims, encoding, requested_precision)};
301280
}
302281

303282
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
304-
py::enum_<tcnn::cpp::EPrecision>(m, "Precision")
305-
.value("Fp32", tcnn::cpp::EPrecision::Fp32)
306-
.value("Fp16", tcnn::cpp::EPrecision::Fp16)
283+
py::enum_<tcnn::cpp::LogSeverity>(m, "LogSeverity")
284+
.value("Info", tcnn::cpp::LogSeverity::Info)
285+
.value("Debug", tcnn::cpp::LogSeverity::Debug)
286+
.value("Warning", tcnn::cpp::LogSeverity::Warning)
287+
.value("Error", tcnn::cpp::LogSeverity::Error)
288+
.value("Success", tcnn::cpp::LogSeverity::Success)
289+
.export_values()
290+
;
291+
292+
py::enum_<tcnn::cpp::Precision>(m, "Precision")
293+
.value("Fp32", tcnn::cpp::Precision::Fp32)
294+
.value("Fp16", tcnn::cpp::Precision::Fp16)
307295
.export_values()
308296
;
309297

310298
m.def("batch_size_granularity", &tcnn::cpp::batch_size_granularity);
299+
m.def("default_loss_scale", &tcnn::cpp::default_loss_scale);
311300
m.def("free_temporary_memory", &tcnn::cpp::free_temporary_memory);
312301
m.def("has_networks", &tcnn::cpp::has_networks);
313302
m.def("preferred_precision", &tcnn::cpp::preferred_precision);
314303

304+
m.def("set_log_callback", &tcnn::cpp::set_log_callback);
305+
315306
// Encapsulates an abstract context of an operation
316307
// (commonly the forward pass) to be passed on to other
317308
// operations (commonly the backward pass).

bindings/torch/tinycudann/modules.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import gc
1010
import importlib
11+
import os
1112
import warnings
1213

1314
import torch
@@ -57,6 +58,14 @@ def _get_system_compute_capability():
5758
if _C is None:
5859
raise EnvironmentError(f"Could not find compatible tinycudann extension for compute capability {system_compute_capability}.")
5960

61+
# Pipe tcnn warnings and errors into Python
62+
# def _log(severity, msg):
63+
# if severity == _C.LogSeverity.Warning:
64+
# warnings.warn(f"tinycudann warning: {msg}")
65+
# elif severity == _C.LogSeverity.Error:
66+
# warnings.warn(f"tinycudann error: {msg}")
67+
68+
# _C.set_log_callback(_log)
6069
def _torch_precision(tcnn_precision):
6170
if tcnn_precision == _C.Precision.Fp16:
6271
return torch.half
@@ -162,7 +171,7 @@ def __init__(self, seed=1337):
162171
self.params = torch.nn.Parameter(initial_params, requires_grad=True)
163172
self.register_parameter(name="params", param=self.params)
164173

165-
self.loss_scale = 128.0 if self.native_tcnn_module.param_precision() == _C.Precision.Fp16 else 1.0
174+
self.loss_scale = _C.default_loss_scale(self.native_tcnn_module.param_precision())
166175

167176
def forward(self, x):
168177
if not x.is_cuda:

0 commit comments

Comments
 (0)