### Install tiny-cuda-nn PyTorch Extension from Local Clone Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md Install the PyTorch extension from a local clone of the tiny-cuda-nn repository. Navigate to the bindings/torch directory before running the installation command. ```sh tiny-cuda-nn$ cd bindings/torch tiny-cuda-nn/bindings/torch$ python setup.py install ``` -------------------------------- ### Install tiny-cuda-nn PyTorch Extension Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md Install the PyTorch extension from a Git repository. Ensure you have a CUDA-enabled PyTorch environment set up. ```sh pip install git+https://github.com/NVlabs/tiny-cuda-nn/#subdirectory=bindings/torch ``` -------------------------------- ### Add Subdirectories for Examples, Benchmarks, and Tests Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/CMakeLists.txt Conditionally adds subdirectories for building examples, benchmarks, and tests based on CMake variables. ```cmake if (TCNN_BUILD_EXAMPLES) add_subdirectory("samples") endif() if (TCNN_BUILD_BENCHMARK) add_subdirectory("benchmarks/image") add_subdirectory("benchmarks/mlp") endif() if (TCNN_BUILD_TESTS) enable_testing() add_subdirectory(tests) list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure") endif() ``` -------------------------------- ### Running MLP Learning an Image Example Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md Command to run the example application that learns a 2D image function. This demonstrates the usage of tiny-cuda-nn for image-based tasks. ```sh tiny-cuda-nn$ ./build/mlp_learning_an_image data/images/albert.jpg data/config_hash.json ``` -------------------------------- ### Installing Build Tools on Linux Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md Installs essential build tools for compiling C++ projects on Debian-based Linux systems. This is a prerequisite for building tiny-cuda-nn. ```sh sudo apt-get install build-essential git ``` -------------------------------- ### Use tiny-cuda-nn Models in PyTorch Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md Example of integrating tiny-cuda-nn models within PyTorch. Demonstrates creating a model using either an efficient Encoding+Network combo or separate, more flexible modules. The optional JIT fusion acceleration is also shown. ```py import commentjson as json import tinycudann as tcnn import torch with open("data/config_hash.json") as f: config = json.load(f) # Option 1: efficient Encoding+Network combo. model = tcnn.NetworkWithInputEncoding( n_input_dims, n_output_dims, config["encoding"], config["network"] ) # Option 2: separate modules. Slower but more flexible. encoding = tcnn.Encoding(n_input_dims, config["encoding"]) network = tcnn.Network(encoding.n_output_dims, n_output_dims, config["network"]) model = torch.nn.Sequential(encoding, network) model.jit_fusion = tcnn.supports_jit_fusion() # Optional: accelerate with JIT fusion ``` -------------------------------- ### Disable FP16 and Install PyTorch Extension Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md Install the PyTorch extension while disabling half-precision (FP16) by setting the TCNN_HALF_PRECISION environment variable to 0. This is useful for debugging or on hardware with slow FP16 support. ```sh # Linux / macOS (Disable FP16) export TCNN_HALF_PRECISION=0 pip install git+https://github.com/NVlabs/tiny-cuda-nn/#subdirectory=bindings/torch ``` -------------------------------- ### Configure Composite Encoding Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Allows combining multiple encoding methods. This example composes TriangleWave, OneBlob, and Identity encodings for different dimension sets. ```json5 { "otype": "Composite", "nested": [ { "n_dims_to_encode": 3, // Spatial dims "otype": "TriangleWave", "n_frequencies": 12 }, { "n_dims_to_encode": 5, // Non-linear appearance dims. "otype": "OneBlob", "n_bins": 4 }, { // Number of remaining linear dims is automatically derived "otype": "Identity" } ] } ``` -------------------------------- ### Autodetect CUDA Architectures Function Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/CMakeLists.txt This CMake function automatically detects the compute capabilities of installed CUDA devices. It writes a small C++ program to detect capabilities, compiles and runs it, and then processes the output. If detection fails, it defaults to Turing and Ampere architectures. ```cmake function(TCNN_AUTODETECT_CUDA_ARCHITECTURES OUT_VARIABLE) if (NOT TCNN_AUTODETECT_CUDA_ARCHITECTURES_OUTPUT) if (CMAKE_CUDA_COMPILER_LOADED) # CUDA as a language set(file "${PROJECT_BINARY_DIR}/detect_tcnn_cuda_architectures.cu") else() set(file "${PROJECT_BINARY_DIR}/detect_tcnn_cuda_architectures.cpp") endif() file(WRITE ${file} "" "#include \n" "#include \n" "int main() {\n" "\tint count = 0;\n" "\tif (cudaSuccess != cudaGetDeviceCount(&count)) return -1;\n" "\tif (count == 0) return -1;\n" "\tfor (int device = 0; device < count; ++device) {\n" "\t\tcudaDeviceProp prop;\n" "\t\tif (cudaSuccess == cudaGetDeviceProperties(&prop, device)) {\n" "\t\t\tstd::printf(\"%d%d\", prop.major, prop.minor);\n" "\t\t\tif (device < count - 1) std::printf(\";\");\n" "\t\t}\n" "\t}\n" "\treturn 0;\n" "}\n" ) try_run(run_result compile_result ${PROJECT_BINARY_DIR} ${file} RUN_OUTPUT_VARIABLE compute_capabilities) if (run_result EQUAL 0) # If the user has multiple GPUs with the same compute capability installed, list that capability only once. list(REMOVE_DUPLICATES compute_capabilities) set(TCNN_AUTODETECT_CUDA_ARCHITECTURES_OUTPUT ${compute_capabilities} CACHE INTERNAL "Returned GPU architectures from detect_gpus tool" FORCE) endif() endif() if (NOT TCNN_AUTODETECT_CUDA_ARCHITECTURES_OUTPUT) message(STATUS "Automatic GPU detection failed. Building for Turing and Ampere as a best guess.") set(${OUT_VARIABLE} "75;86" PARENT_SCOPE) else () set(${OUT_VARIABLE} ${TCNN_AUTODETECT_CUDA_ARCHITECTURES_OUTPUT} PARENT_SCOPE) endif() endfunction() ``` -------------------------------- ### Build MLP Learning Sample Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/samples/CMakeLists.txt Configures and builds the `mlp_learning_an_image` executable. This sample demonstrates learning an image using a Multi-Layer Perceptron with tiny-cuda-nn. ```cmake add_executable(mlp_learning_an_image mlp_learning_an_image.cu ../dependencies/stbi/stbi_wrapper.cpp) target_link_libraries(mlp_learning_an_image PUBLIC ${CUDA_LIBRARIES} tiny-cuda-nn) ``` -------------------------------- ### Average Wrapper Optimizer Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Wraps another optimizer to compute a linear average of parameters over the last N steps for inference only. Requires a nested optimizer configuration. ```json5 { "otype": "Average", // Component type. "n_samples": 128, // The number of steps to be averaged over. "nested": { // The nested optimizer. "otype": "Adam" } } ``` -------------------------------- ### Configure, Train, and Use Tiny CUDA Neural Network Model Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md This C++ snippet demonstrates how to configure a neural network model using a JSON object, train it on a GPU, and perform inference. Ensure batch_size is a multiple of tcnn::BATCH_SIZE_GRANULARITY for training. ```cpp #include // Configure the model nlohmann::json config = { {"loss", { {"otype", "L2"} }}, {"optimizer", { {"otype", "Adam"}, {"learning_rate", 1e-3}, }}, {"encoding", { {"otype", "HashGrid"}, {"n_levels", 16}, {"n_features_per_level", 2}, {"log2_hashmap_size", 19}, {"base_resolution", 16}, {"per_level_scale", 2.0}, }}, {"network", { {"otype", "FullyFusedMLP"}, {"activation", "ReLU"}, {"output_activation", "None"}, {"n_neurons", 64}, {"n_hidden_layers", 2}, }}, }; using namespace tcnn; auto model = create_from_config(n_input_dims, n_output_dims, config); model->set_jit_fusion(supports_jit_fusion()); // Optional: accelerate with JIT fusion // Train the model (batch_size must be a multiple of tcnn::BATCH_SIZE_GRANULARITY) GPUMatrix training_batch_inputs(n_input_dims, batch_size); GPUMatrix training_batch_targets(n_output_dims, batch_size); for (int i = 0; i < n_training_steps; ++i) { generate_training_batch(&training_batch_inputs, &training_batch_targets); // <-- your code float loss; model.trainer->training_step(training_batch_inputs, training_batch_targets, &loss); std::cout << "iteration=" << i << " loss=" << loss << std::endl; } // Use the model GPUMatrix inference_inputs(n_input_dims, batch_size); generate_inputs(&inference_inputs); // <-- your code GPUMatrix inference_outputs(n_output_dims, batch_size); model.network->inference(inference_inputs, inference_outputs); ``` -------------------------------- ### Stochastic Gradient Descent (SGD) Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Configuration for standard stochastic gradient descent (SGD). Includes learning rate and L2 regularization. ```json5 { "otype": "SGD", // Component type. "learning_rate": 1e-3, // Learning rate. "l2_reg": 1e-8 // Strength of L2 regularization. } ``` -------------------------------- ### Lookahead Optimizer Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Configure the Lookahead optimizer wrapper. Set the lookahead fraction, steps, and the nested optimizer. ```json5 { "otype": "Lookahead", // Component type. "alpha": 0.5, "n_steps": 16, "nested": { "otype": "Adam" } } ``` -------------------------------- ### Enable JIT Fusion in C++ Tiny CUDA Neural Network Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md This C++ snippet shows how to enable JIT fusion for a tiny-cuda-nn model if the system supports it. JIT fusion can provide a significant performance boost. ```cpp auto model = tcnn::create_from_config(...); model->set_jit_fusion(tcnn::supports_jit_fusion()); // Enable JIT if the system supports it ``` -------------------------------- ### Shampoo Optimizer Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Configuration for the Shampoo optimizer, a 2nd order optimizer. Includes learning rate, multiple beta parameters, epsilon, and normalization options. ```json5 { "otype": "Shampoo", // Component type. "learning_rate": 1e-3, // Learning rate. "beta1": 0.9, // Beta1 parameter similar to Adam. // Used to exponentially average the // first gradient moment. "beta2": 0.99, // Beta2 parameter similar to Adam. // Used to exponentially average the // second gradient moment. "beta3": 0.9, // Used to exponentially average L and R. "beta_shampoo": 0.9, // Used to exponentially average // Shampoo updates. "epsilon": 1e-8, // Epsilon parameter similar Adam. // Used to avoid singularity when computing // momentum. "identity": 0.01, // Blends L and R with I*identity for // numerical stability. "cg_on_momentum": true, // Whether to estimate L and R from the // estimated momentum or from the raw // gradients. "l2_reg": 1e-5, // Strength of L2 regularization // applied to the to-be-optimized params. "relative_decay": 0, // Percentage of weights lost per step. "absolute_decay": 0, // Amount of weights lost per step. "frobenius_normalization": true, // Whether to normalize update // steps by the would-be Adam // update's Frobenius norm. } ``` -------------------------------- ### Exponential Decay Optimizer Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Configure the Exponential Decay optimizer wrapper. Define decay parameters and the nested optimizer. ```json5 { "otype": "ExponentialDecay", // Component type. "decay_base": 0.1, "decay_start": 10000, "decay_end": 10000000, "decay_interval": 10000, "nested": { "otype": "Adam" } } ``` -------------------------------- ### Building tiny-cuda-nn with CMake Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md Configures and builds the tiny-cuda-nn project using CMake. The RelWithDebInfo build type is recommended for a balance of performance and debugging information. Use -j for parallel compilation. ```sh tiny-cuda-nn$ cmake . -B build -DCMAKE_BUILD_TYPE=RelWithDebInfo tiny-cuda-nn$ cmake --build build --config RelWithDebInfo -j ``` -------------------------------- ### Exponential Moving Average (EMA) Optimizer Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Configure the EMA optimizer wrapper. Specify the decay rate and the nested optimizer. ```json5 { "otype": "EMA", // Component type. "decay": 0.99, // The EMA's decay per step. "nested": { "otype": "Adam" } } ``` -------------------------------- ### Batched Wrapper Optimizer Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Wraps another optimizer to invoke the nested optimizer once every N steps on the averaged gradient, effectively increasing batch size without increasing memory. Requires a nested optimizer configuration. ```json5 { "otype": "Batched", // Component type. "batch_size_multiplier": 16, // N from the above description "nested": { // The nested optimizer. "otype": "Adam" } } ``` -------------------------------- ### Enable JIT Fusion in Python Tiny CUDA Neural Network Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md This Python snippet demonstrates how to enable JIT fusion for a tiny-cuda-nn model using its PyTorch bindings. Note that the speed-up might be lower compared to the C++ API. ```python import tinycudann as tcnn model = tcnn.NetworkWithInputEncoding(...) # Or any other tcnn model model.jit_fusion = tcnn.supports_jit_fusion() # Enable JIT if the system supports it ``` -------------------------------- ### Fetch and Include CUDA Headers for Runtime Compiler Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/CMakeLists.txt Fetches CUDA headers required by the runtime compiler and includes them with the compiled binary. It attempts to find CUDA include directories and falls back to a relative path if necessary. ```cmake if (TCNN_BUILD_WITH_RTC) # Fetch CUDA headers and folders that will be required by the runtime compiler # and include those headers with the compiled binary of tcnn. foreach (CUDA_INCLUDE_CANDIDATE IN LISTS CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES) if (EXISTS "${CUDA_INCLUDE_CANDIDATE}/cuda_fp16.h") set(CUDA_INCLUDE "${CUDA_INCLUDE_CANDIDATE}") break() endif() endforeach(CUDA_INCLUDE_CANDIDATE) if (NOT CUDA_INCLUDE) # If the CUDA include dir couldn't be found via CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES, # try a relative path w.r.t. the CUDA compiler binary as a last-ditch effort. get_filename_component(CUDA_COMPILER_BIN "${CMAKE_CUDA_COMPILER}" DIRECTORY) get_filename_component(CUDA_DIR "${CUDA_COMPILER_BIN}" DIRECTORY) set(CUDA_INCLUDE "${CUDA_DIR}/include") endif() file(GLOB CUDA_HEADERS "${CUDA_INCLUDE}/cuda_fp16*" "${CUDA_INCLUDE}/vector*") if (NOT CUDA_HEADERS) message(WARNING "FP16 headers could not be found. JIT compilation will likely fail.") endif() file(GLOB_RECURSE TCNN_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/include/tiny-cuda-nn/*") file(GLOB PCG32_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/dependencies/pcg32/*") cmrc_add_resources(tiny-cuda-nn-resources WHENCE "${CUDA_INCLUDE}" ${CUDA_HEADERS}) cmrc_add_resources(tiny-cuda-nn-resources WHENCE "${CMAKE_CURRENT_SOURCE_DIR}/include" ${TCNN_HEADERS}) cmrc_add_resources(tiny-cuda-nn-resources WHENCE "${CMAKE_CURRENT_SOURCE_DIR}/dependencies" ${PCG32_HEADERS}) endif() ``` -------------------------------- ### Cloning tiny-cuda-nn Repository Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md Clones the tiny-cuda-nn repository and its submodules. This command must be run before building the project. ```sh $ git clone --recursive https://github.com/nvlabs/tiny-cuda-nn $ cd tiny-cuda-nn ``` -------------------------------- ### Novograd Optimizer Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Configuration for the Novograd optimizer. Includes learning rate, momentum parameters, and decay settings. ```json5 { "otype": "Novograd", // Component type. "learning_rate": 1e-3, // Learning rate. "beta1": 0.9, // Beta1 parameter of Novograd. "beta2": 0.999, // Beta2 parameter of Novograd. "epsilon": 1e-8, // Epsilon parameter of Novograd. "relative_decay": 0, // Percentage of weights lost per step. "absolute_decay": 0 // Amount of weights lost per step. } ``` -------------------------------- ### Manual JIT Fusion Kernel Integration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md Integrates a tiny-cuda-nn model into a larger CUDA kernel using runtime compilation. Ensure all threads in a warp are active when calling the model function. ```cpp #include auto model = tcnn::create_from_config(32 /* input dims */, 16 /* output dims */, ...); auto fused_kernel = tcnn::CudaRtcKernel( "your_kernel", fmt::format(R"( {MODEL_DEVICE_FUNCTION} __global__ void your_kernel(...) { // Get input to model from either registers or memory. tcnn::hvec<32> input = ...; // Call tiny-cuda-nn model. All 32 threads of the warp must be active here. tcnn::hvec<16> output = model_fun(nerf_in, params); // Do something with the model output. }") , fmt::arg("MODEL_DEVICE_FUNCTION", model->generate_device_function("model_fun")) ) ); uint32_t blocks = 1; uint32_t threads = 128; // Must be multiple of 32 for neural networks to work. uint32_t shmem_size = 0; // Can be any size that your_kernel needs. cudaStream_t stream = nullptr; // Can be any stream. fused_kernel.launch(blocks, threads, shmem_size, stream, ... /* params of your_kernel */); ``` -------------------------------- ### Define Image Benchmarking Executable Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/benchmarks/image/CMakeLists.txt Defines the executable for the custom image benchmarking tool, linking necessary CUDA and tiny-cuda-nn libraries. ```cmake add_executable(bench_image_ours bench_ours.cu ../../dependencies/stbi/stbi_wrapper.cpp) target_link_libraries(bench_image_ours PUBLIC ${CUDA_LIBRARIES} tiny-cuda-nn cublas) ``` -------------------------------- ### BibTeX Citation for tiny-cuda-nn Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md Use this BibTeX entry for citing the tiny-cuda-nn framework in academic publications. Ensure the version and year are up-to-date. ```bibtex @software{tiny-cuda-nn, author = {M"uller, Thomas}, license = {BSD-3-Clause}, month = {4}, title = {{tiny-cuda-nn}}, url = {https://github.com/NVlabs/tiny-cuda-nn}, version = {2.0}, year = {2021} } ``` -------------------------------- ### Create Specific Test Executables Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/tests/CMakeLists.txt These lines call the create_test function to generate individual test executables for different components of the library. ```cmake create_test(test_encodings) ``` ```cmake create_test(test_grid) ``` ```cmake create_test(test_jit_losses) ``` ```cmake create_test(test_networks) ``` -------------------------------- ### Adam Optimizer Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Configuration for the Adam optimizer, generalized to AdaBound. Includes learning rate, momentum parameters, L2 regularization, and decay settings. ```json5 { "otype": "Adam", // Component type. "learning_rate": 1e-3, // Learning rate. "beta1": 0.9, // Beta1 parameter of Adam. "beta2": 0.999, // Beta2 parameter of Adam. "epsilon": 1e-8, // Epsilon parameter of Adam. "l2_reg": 1e-8, // Strength of L2 regularization // applied to the to-be-optimized params. "relative_decay": 0, // Percentage of weights lost per step. "absolute_decay": 0, // Amount of weights lost per step. "adabound": false // Whether to enable AdaBound. } ``` -------------------------------- ### Define Test Creation Function Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/tests/CMakeLists.txt This function defines a reusable way to create test executables. It compiles a CUDA source file and links it against the tiny-cuda-nn library. ```cmake function (create_test TEST_NAME) add_executable(${TEST_NAME} ${TEST_NAME}.cu) target_link_libraries(${TEST_NAME} tiny-cuda-nn) add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME}) endfunction() ``` -------------------------------- ### Grid Encoding Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Configures a trainable multiresolution grid for encoding. Supports Hash, Tiled, or Dense storage types with various parameters for resolution, levels, and interpolation. ```json { "otype": "Grid", // Component type. "type": "Hash", // Type of backing storage of the // grids. Can be "Hash", "Tiled" // or "Dense". "n_levels": 16, // Number of levels (resolutions) "n_features_per_level": 2, // Dimensionality of feature vector // stored in each level's entries. "log2_hashmap_size": 19, // If type is "Hash", is the base-2 // logarithm of the number of elements // in each backing hash table. "base_resolution": 16, // The resolution of the coarsest le- // vel is base_resolution^input_dims. "per_level_scale": 2.0, // The geometric growth factor, i.e. // the factor by which the resolution // of each grid is larger (per axis) // than that of the preceding level. "interpolation": "Linear" // How to interpolate nearby grid // lookups. Can be "Nearest", "Linear", // or "Smoothstep" (for smooth deri- // vatives). } ``` -------------------------------- ### TriangleWave Encoding Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Similar to Frequency encoding but uses a triangle wave for cheaper computation. Works well with high dynamic range but can suffer from stripe artifacts. ```json { "otype": "TriangleWave", // Component type. "n_frequencies": 12 // Number of frequencies (triwave) // per encoded dimension. } ``` -------------------------------- ### Add Tiny CUDA NN Static Library Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/CMakeLists.txt Adds the tiny-cuda-nn library as a static library and configures its compile definitions, options, include directories, and linked libraries. ```cmake add_library(tiny-cuda-nn STATIC ${TCNN_SOURCES}) target_compile_definitions(tiny-cuda-nn PUBLIC ${TCNN_DEFINITIONS}) target_compile_options(tiny-cuda-nn PUBLIC $<$:${CUDA_NVCC_FLAGS}>) target_include_directories(tiny-cuda-nn PUBLIC ${TCNN_INCLUDES}) target_link_libraries(tiny-cuda-nn PUBLIC ${TCNN_LIBRARIES}) ``` -------------------------------- ### L2 Loss Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Standard L2 loss function configuration. ```json { "otype": "L2" // Component type. } ``` -------------------------------- ### Link Libraries for MLP Benchmark Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/benchmarks/mlp/CMakeLists.txt Links the `bench_mlp_ours` target with essential libraries including CUDA, tiny-cuda-nn, and cublas. Ensure these libraries are available in your build environment. ```cmake target_link_libraries(bench_mlp_ours PUBLIC ${CUDA_LIBRARIES} tiny-cuda-nn cublas) ``` -------------------------------- ### L1 Loss Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Standard L1 loss function configuration. ```json { "otype": "L1" // Component type. } ``` -------------------------------- ### Set CUDA Standard and Extensions Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/CMakeLists.txt Configures the CUDA standard to be used (e.g., 17) and ensures it's required, while disabling extensions for broader compatibility. ```cmake set(CMAKE_CUDA_STANDARD 17) set(CMAKE_CUDA_STANDARD_REQUIRED ON) set(CMAKE_CUDA_EXTENSIONS OFF) set(CUDA_LINK_LIBRARIES_KEYWORD PUBLIC) ``` -------------------------------- ### Configuring CUDA Environment Variables on Linux Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/README.md Sets the PATH and LD_LIBRARY_PATH environment variables to include the CUDA toolkit. This ensures that the system can find and use CUDA executables and libraries. ```sh export PATH="/usr/local/cuda-12.6.3/bin:$PATH" export LD_LIBRARY_PATH="/usr/local/cuda-12.6.3/lib64:$LD_LIBRARY_PATH" ``` -------------------------------- ### Define Library Includes Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/CMakeLists.txt Appends include directories to the TCNN_INCLUDES list, which will be used when compiling the tiny-cuda-nn library. ```cmake list(APPEND TCNN_INCLUDES "include" "dependencies" "dependencies/cutlass/include" "dependencies/cutlass/tools/util/include" ) ``` -------------------------------- ### MAPE Loss Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Mean absolute percentage error (MAPE) loss, normalized by the target. ```json { "otype": "MAPE" // Component type. } ``` -------------------------------- ### Configure CUTLASS MLP Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Defines a Multi-Layer Perceptron (MLP) using CUTLASS GEMM routines. This MLP supports arbitrary numbers of neurons and hidden layers. ```json5 { "otype": "CutlassMLP", // Component type. "activation": "ReLU", // Activation of hidden layers. "output_activation": "None", // Activation of the output layer. "n_neurons": 128, // Neurons in each hidden layer. "n_hidden_layers": 5 // Number of hidden layers. } ``` -------------------------------- ### Add MLP Benchmarking Executable Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/benchmarks/mlp/CMakeLists.txt Adds the `bench_mlp_ours` executable to the CMake build system. This is the primary step for compiling the MLP benchmark. ```cmake add_executable(bench_mlp_ours bench_mlp_ours.cu) ``` -------------------------------- ### Cross Entropy Loss Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Standard cross entropy loss. Applicable when network prediction is a probability density function. ```json5 { "otype": "CrossEntropy" // Component type. } ``` -------------------------------- ### Relative L1 Loss Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Relative L1 loss normalized by the network prediction. ```json { "otype": "RelativeL1" // Component type. } ``` -------------------------------- ### Relative L2 Luminance Loss Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Use for normalizing loss by network prediction luminance. Applicable when network prediction is RGB. ```json5 { "otype": "RelativeL2Luminance" // Component type. } ``` -------------------------------- ### Relative L2 Loss Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Relative L2 loss normalized by the network prediction. ```json { "otype": "RelativeL2" // Component type. } ``` -------------------------------- ### SMAPE Loss Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Symmetric mean absolute percentage error (SMAPE) loss, normalized by the mean of the prediction and the target. ```json { "otype": "SMAPE" // Component type. } ``` -------------------------------- ### Variance Loss Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Standard variance loss. Applicable when network prediction is a probability density function. ```json5 { "otype": "Variance" // Component type. } ``` -------------------------------- ### Configure Frequency Encoding Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Defines a Frequency encoding, suitable for high dynamic range dimensions and inspired by NeRF. The number of encoded dimensions is twice the number of frequencies. ```json5 { "otype": "Frequency", // Component type. "n_frequencies": 12 // Number of frequencies (sin & cos) // per encoded dimension. } ``` -------------------------------- ### Configure Fully Fused MLP Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Defines a Fully Fused Multi-Layer Perceptron (MLP). Hidden layer sizes are restricted to 16, 32, 64, or 128 neurons. ```json5 { "otype": "FullyFusedMLP", // Component type. "activation": "ReLU", // Activation of hidden layers. "output_activation": "None", // Activation of the output layer. "n_neurons": 128, // Neurons in each hidden layer. // May only be 16, 32, 64, or 128. "n_hidden_layers": 5 // Number of hidden layers. } ``` -------------------------------- ### OneBlob Encoding Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md An encoding that results in a more accurate fit than identity when the dynamic range is limited, without stripe artifacts. Uses a quartic kernel for performance. ```json { "otype": "OneBlob", // Component type. "n_bins": 16 // Number of bins per encoded dimension. } ``` -------------------------------- ### Determine CUDA Architectures Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/CMakeLists.txt Sets the CMAKE_CUDA_ARCHITECTURES based on environment variables, CMake variables, or automatic detection. It prioritizes environment variables, then CMake variables, and finally falls back to automatic detection. ```cmake get_directory_property(TCNN_HAS_PARENT PARENT_DIRECTORY) if (DEFINED ENV{TCNN_CUDA_ARCHITECTURES}) message(STATUS "Obtained CUDA architectures from environment variable TCNN_CUDA_ARCHITECTURES=$ENV{TCNN_CUDA_ARCHITECTURES}") set(CMAKE_CUDA_ARCHITECTURES $ENV{TCNN_CUDA_ARCHITECTURES}) elseif (TCNN_CUDA_ARCHITECTURES) message(STATUS "Obtained CUDA architectures from CMake variable TCNN_CUDA_ARCHITECTURES=${TCNN_CUDA_ARCHITECTURES}") set(CMAKE_CUDA_ARCHITECTURES ${TCNN_CUDA_ARCHITECTURES}) else () message(STATUS "Obtained CUDA architectures automatically from installed GPUs") TCNN_AUTODETECT_CUDA_ARCHITECTURES(CMAKE_CUDA_ARCHITECTURES) endif() ``` -------------------------------- ### Specify Activation Function Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Sets the activation function for network layers. Supported functions include ReLU, LeakyReLU, SiLU, and others. ```json5 { "activation": "ReLU" } ``` -------------------------------- ### Identity Encoding Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md Leaves values untouched. Optionally, multiplies each dimension by a scalar and adds an offset. ```json { "otype": "Identity", // Component type. "scale": 1.0, // Scaling of each encoded dimension. "offset": 0.0 // Added to each encoded dimension. } ``` -------------------------------- ### Spherical Harmonics Encoding Configuration Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/DOCUMENTATION.md A frequency-space encoding suitable for direction vectors. Expects 3D inputs transformed into the unit cube. ```json { "otype": "SphericalHarmonics", // Component type. "degree": 4 // The SH degree up to which // to evaluate the encoding. // Produces degree^2 encoded // dimensions. } ``` -------------------------------- ### Adjust CUDA Architectures Based on CUDA Version Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/CMakeLists.txt This section adjusts the list of target CUDA architectures based on the detected CUDA compiler version. It ensures that architectures are compatible with the CUDA toolkit version, targeting the latest supported architecture for newer CUDA versions and the earliest for older ones. ```cmake # If the CUDA version does not support the chosen architecture, target # the latest supported one instead. if (CUDA_VERSION VERSION_LESS 11.0) set(LATEST_SUPPORTED_CUDA_ARCHITECTURE 75) elseif (CUDA_VERSION VERSION_LESS 11.1) set(LATEST_SUPPORTED_CUDA_ARCHITECTURE 80) elseif (CUDA_VERSION VERSION_LESS 11.8) set(LATEST_SUPPORTED_CUDA_ARCHITECTURE 86) elseif (CUDA_VERSION VERSION_LESS 12.8) set(LATEST_SUPPORTED_CUDA_ARCHITECTURE 90) else () set(LATEST_SUPPORTED_CUDA_ARCHITECTURE 120) endif() if (CUDA_VERSION VERSION_GREATER_EQUAL 13.0) set(EARLIEST_SUPPORTED_CUDA_ARCHITECTURE 75) elseif (CUDA_VERSION VERSION_GREATER_EQUAL 12.0) set(EARLIEST_SUPPORTED_CUDA_ARCHITECTURE 50) else () set(EARLIEST_SUPPORTED_CUDA_ARCHITECTURE 20) endif() foreach (CUDA_CC IN LISTS CMAKE_CUDA_ARCHITECTURES) if (CUDA_CC GREATER ${LATEST_SUPPORTED_CUDA_ARCHITECTURE}) message(WARNING "CUDA version ${CUDA_VERSION} is too low for detected architecture ${CUDA_CC}. Targeting the highest supported architecture ${LATEST_SUPPORTED_CUDA_ARCHITECTURE} instead.") list(REMOVE_ITEM CMAKE_CUDA_ARCHITECTURES ${CUDA_CC}) if (NOT CMAKE_CUDA_ARCHITECTURES) list(APPEND CMAKE_CUDA_ARCHITECTURES ${LATEST_SUPPORTED_CUDA_ARCHITECTURE}) endif() endif () if (CUDA_CC LESS ${EARLIEST_SUPPORTED_CUDA_ARCHITECTURE}) message(ERROR "CUDA version ${CUDA_VERSION} no longer supports detected architecture ${CUDA_CC}. Targeting the lowest supported architecture ${EARLIEST_SUPPORTED_CUDA_ARCHITECTURE} instead.") list(REMOVE_ITEM CMAKE_CUDA_ARCHITECTURES ${CUDA_CC}) if (NOT CMAKE_CUDA_ARCHITECTURES) list(APPEND CMAKE_CUDA_ARCHITECTURES ${EARLIEST_SUPPORTED_CUDA_ARCHITECTURE}) endif() endif () endforeach(CUDA_CC) if (NOT CMAKE_CUDA_ARCHITECTURES) list(APPEND CMAKE_CUDA_ARCHITECTURES ${LATEST_SUPPORTED_CUDA_ARCHITECTURE}) endif() ``` -------------------------------- ### Add Resource Library with cmrc Source: https://github.com/nvlabs/tiny-cuda-nn/blob/master/CMakeLists.txt Uses cmrc to add a resource library for CUDA headers and dependencies. This is typically used when runtime compilation is enabled. ```cmake include("${CMAKE_CURRENT_SOURCE_DIR}/dependencies/cmrc/CMakeRC.cmake") cmrc_add_resource_library(tiny-cuda-nn-resources NAMESPACE tcnn) list(APPEND TCNN_DEFINITIONS -DTCNN_CMRC) list(APPEND TCNN_LIBRARIES tiny-cuda-nn-resources) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.