Skip to content

Commit

Permalink
Merge pull request #1399 from NVlabs/replace-glm
Browse files Browse the repository at this point in the history
Replace GLM with tcnn's vector math, bugfixes and extra bindings
  • Loading branch information
Tom94 authored Jul 9, 2023
2 parents 8ff8075 + b9b7753 commit 090aed6
Show file tree
Hide file tree
Showing 60 changed files with 2,023 additions and 2,319 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,3 @@
[submodule "dependencies/OpenXR-SDK"]
path = dependencies/OpenXR-SDK
url = https://github.com/KhronosGroup/OpenXR-SDK.git
[submodule "dependencies/glm"]
path = dependencies/glm
url = https://github.com/g-truc/glm
10 changes: 4 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_CRT_SECURE_NO_WARNINGS")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP24")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fms-extensions")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
endif()

Expand All @@ -76,7 +75,6 @@ if (MSVC)
else()
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler=-Wno-float-conversion")
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler=-fno-strict-aliasing")
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler=-fms-extensions")
list(APPEND CUDA_NVCC_FLAGS "-Xcompiler=-fPIC")
endif()
list(APPEND CUDA_NVCC_FLAGS "--extended-lambda")
Expand Down Expand Up @@ -203,7 +201,6 @@ endif(NGP_BUILD_WITH_GUI)
list(APPEND NGP_INCLUDE_DIRECTORIES
"dependencies"
"dependencies/filesystem"
"dependencies/glm"
"dependencies/nanovdb"
"dependencies/NaturalSort"
"dependencies/tinylogger"
Expand Down Expand Up @@ -261,8 +258,7 @@ endif()
list(APPEND NGP_SOURCES
${GUI_SOURCES}
src/camera_path.cu
src/common.cu
src/common_device.cu
src/common_host.cu
src/marching_cubes.cu
src/nerf_loader.cu
src/render_buffer.cu
Expand All @@ -273,7 +269,7 @@ list(APPEND NGP_SOURCES
src/testbed_volume.cu
src/thread_pool.cpp
src/tinyexr_wrapper.cu
src/tinyobj_loader_wrapper.cpp
src/tinyobj_loader_wrapper.cu
src/triangle_bvh.cu
)

Expand All @@ -284,6 +280,8 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR})

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")

if (NGP_OPTIX)
add_library(optix_program OBJECT
Expand Down
1 change: 0 additions & 1 deletion dependencies/glm
Submodule glm deleted from efec5d
2 changes: 1 addition & 1 deletion dependencies/tiny-cuda-nn
Submodule tiny-cuda-nn updated 81 files
+10 −10 .github/workflows/main.yml
+2 −0 .gitignore
+2 −13 CMakeLists.txt
+6 −3 DOCUMENTATION.md
+1 −1 README.md
+0 −2 benchmarks/image/bench_ours.cu
+27 −12 bindings/torch/setup.py
+29 −38 bindings/torch/tinycudann/bindings.cpp
+10 −1 bindings/torch/tinycudann/modules.py
+14 −0 dependencies/pcg32/LICENSE.txt
+3 −9 dependencies/pcg32/pcg32.h
+1 −1 dependencies/pybind11_json/LICENSE
+37 −0 dependencies/stbi/LICENSE
+160 −436 include/tiny-cuda-nn/common.h
+472 −100 include/tiny-cuda-nn/common_device.h
+434 −0 include/tiny-cuda-nn/common_host.h
+4 −2 include/tiny-cuda-nn/config.h
+21 −14 include/tiny-cuda-nn/cpp_api.h
+5 −5 include/tiny-cuda-nn/cuda_graph.h
+2 −4 include/tiny-cuda-nn/cutlass_matmul.h
+12 −23 include/tiny-cuda-nn/encoding.h
+5 −6 include/tiny-cuda-nn/encodings/composite.h
+3 −3 include/tiny-cuda-nn/encodings/empty.h
+3 −17 include/tiny-cuda-nn/encodings/frequency.h
+52 −322 include/tiny-cuda-nn/encodings/grid.h
+126 −0 include/tiny-cuda-nn/encodings/grid_interface.h
+3 −3 include/tiny-cuda-nn/encodings/identity.h
+8 −8 include/tiny-cuda-nn/encodings/oneblob.h
+11 −304 include/tiny-cuda-nn/encodings/spherical_harmonics.h
+3 −3 include/tiny-cuda-nn/encodings/triangle_wave.h
+30 −36 include/tiny-cuda-nn/gpu_matrix.h
+30 −28 include/tiny-cuda-nn/gpu_memory.h
+2 −2 include/tiny-cuda-nn/gpu_memory_json.h
+20 −4 include/tiny-cuda-nn/loss.h
+123 −0 include/tiny-cuda-nn/losses/constant.h
+10 −8 include/tiny-cuda-nn/losses/cross_entropy.h
+8 −6 include/tiny-cuda-nn/losses/l1.h
+8 −6 include/tiny-cuda-nn/losses/l2.h
+9 −7 include/tiny-cuda-nn/losses/mape.h
+8 −6 include/tiny-cuda-nn/losses/relative_l1.h
+8 −6 include/tiny-cuda-nn/losses/relative_l2.h
+8 −6 include/tiny-cuda-nn/losses/relative_l2_luminance.h
+9 −7 include/tiny-cuda-nn/losses/smape.h
+10 −8 include/tiny-cuda-nn/losses/variance_is.h
+2 −2 include/tiny-cuda-nn/matrix_layout.h
+2 −2 include/tiny-cuda-nn/multi_stream.h
+73 −6 include/tiny-cuda-nn/network.h
+6 −4 include/tiny-cuda-nn/network_with_input_encoding.h
+3 −3 include/tiny-cuda-nn/networks/cutlass_mlp.h
+3 −3 include/tiny-cuda-nn/networks/fully_fused_mlp.h
+33 −27 include/tiny-cuda-nn/object.h
+3 −3 include/tiny-cuda-nn/optimizer.h
+2 −2 include/tiny-cuda-nn/optimizers/adam.h
+2 −2 include/tiny-cuda-nn/optimizers/average.h
+2 −2 include/tiny-cuda-nn/optimizers/batched.h
+2 −2 include/tiny-cuda-nn/optimizers/composite.h
+2 −2 include/tiny-cuda-nn/optimizers/ema.h
+2 −2 include/tiny-cuda-nn/optimizers/exponential_decay.h
+2 −2 include/tiny-cuda-nn/optimizers/lookahead.h
+5 −6 include/tiny-cuda-nn/optimizers/novograd.h
+2 −2 include/tiny-cuda-nn/optimizers/sgd.h
+8 −9 include/tiny-cuda-nn/optimizers/shampoo.h
+3 −30 include/tiny-cuda-nn/random.h
+4 −6 include/tiny-cuda-nn/reduce_sum.h
+56 −48 include/tiny-cuda-nn/trainer.h
+1,188 −0 include/tiny-cuda-nn/vec.h
+63 −7 include/tiny-cuda-nn/vec_json.h
+130 −0 include/tiny-cuda-nn/vec_pybind11.h
+2 −2 samples/mlp_learning_an_image.cu
+2 −0 samples/mlp_learning_an_image_pytorch.py
+0 −122 src/common.cu
+340 −0 src/common_host.cu
+20 −41 src/cpp_api.cu
+8 −8 src/cutlass_mlp.cu
+38 −67 src/encoding.cu
+7 −7 src/fully_fused_mlp.cu
+55 −22 src/loss.cu
+13 −45 src/network.cu
+4 −5 src/object.cu
+2 −2 src/optimizer.cu
+2 −2 src/reduce_sum.cu
6 changes: 3 additions & 3 deletions include/neural-graphics-primitives/adam_optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include <json/json.hpp>

NGP_NAMESPACE_BEGIN
namespace ngp {

class VarAdamOptimizer {
public:
Expand Down Expand Up @@ -241,7 +241,7 @@ class RotationAdamOptimizer {
float actual_learning_rate = m_hparams.learning_rate * std::sqrt(1 - std::pow(m_hparams.beta2, m_state.iter)) / (1 - std::pow(m_hparams.beta1, m_state.iter));
m_state.first_moment = m_hparams.beta1 * m_state.first_moment + (1 - m_hparams.beta1) * gradient;
m_state.second_moment = m_hparams.beta2 * m_state.second_moment + (1 - m_hparams.beta2) * gradient * gradient;
vec3 rot = actual_learning_rate * m_state.first_moment / (sqrt(m_state.second_moment) + vec3(m_hparams.epsilon));
vec3 rot = actual_learning_rate * m_state.first_moment / (sqrt(m_state.second_moment) + m_hparams.epsilon);

m_state.variable = rotvec(rotmat(-rot) * rotmat(variable()));
}
Expand Down Expand Up @@ -308,4 +308,4 @@ inline void from_json(const nlohmann::json& j, RotationAdamOptimizer& opt) {
opt.from_json(j);
}

NGP_NAMESPACE_END
}
36 changes: 14 additions & 22 deletions include/neural-graphics-primitives/bounding_box.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <neural-graphics-primitives/common_device.cuh>
#include <neural-graphics-primitives/triangle.cuh>

NGP_NAMESPACE_BEGIN
namespace ngp {

template <int N_POINTS>
NGP_HOST_DEVICE inline void project(vec3 points[N_POINTS], const vec3& axis, float& min, float& max) {
Expand Down Expand Up @@ -51,16 +51,16 @@ struct BoundingBox {
enlarge(tri.c);
}

BoundingBox(std::vector<Triangle>::iterator begin, std::vector<Triangle>::iterator end) {
NGP_HOST_DEVICE BoundingBox(Triangle* begin, Triangle* end) {
min = max = begin->a;
for (auto it = begin; it != end; ++it) {
enlarge(*it);
}
}

NGP_HOST_DEVICE void enlarge(const BoundingBox& other) {
min = glm::min(min, other.min);
max = glm::max(max, other.max);
min = tcnn::min(min, other.min);
max = tcnn::max(max, other.max);
}

NGP_HOST_DEVICE void enlarge(const Triangle& tri) {
Expand All @@ -70,8 +70,8 @@ struct BoundingBox {
}

NGP_HOST_DEVICE void enlarge(const vec3& point) {
min = glm::min(min, point);
max = glm::max(max, point);
min = tcnn::min(min, point);
max = tcnn::max(max, point);
}

NGP_HOST_DEVICE void inflate(float amount) {
Expand All @@ -93,8 +93,8 @@ struct BoundingBox {

NGP_HOST_DEVICE BoundingBox intersection(const BoundingBox& other) const {
BoundingBox result = *this;
result.min = glm::max(result.min, other.min);
result.max = glm::min(result.max, other.max);
result.min = tcnn::max(result.min, other.min);
result.max = tcnn::min(result.max, other.max);
return result;
}

Expand Down Expand Up @@ -165,14 +165,14 @@ struct BoundingBox {
float tmax = (max.x - pos.x) / dir.x;

if (tmin > tmax) {
tcnn::host_device_swap(tmin, tmax);
host_device_swap(tmin, tmax);
}

float tymin = (min.y - pos.y) / dir.y;
float tymax = (max.y - pos.y) / dir.y;

if (tymin > tymax) {
tcnn::host_device_swap(tymin, tymax);
host_device_swap(tymin, tymax);
}

if (tmin > tymax || tymin > tmax) {
Expand All @@ -191,7 +191,7 @@ struct BoundingBox {
float tzmax = (max.z - pos.z) / dir.z;

if (tzmin > tzmax) {
tcnn::host_device_swap(tzmin, tzmax);
host_device_swap(tzmin, tzmax);
}

if (tmin > tzmax || tzmin > tmax) {
Expand All @@ -210,7 +210,7 @@ struct BoundingBox {
}

NGP_HOST_DEVICE bool is_empty() const {
return any(lessThan(max, min));
return max.x < min.x || max.y < min.y || max.z < min.z;
}

NGP_HOST_DEVICE bool contains(const vec3& p) const {
Expand All @@ -226,12 +226,12 @@ struct BoundingBox {
}

NGP_HOST_DEVICE float distance_sq(const vec3& p) const {
return length2(glm::max(glm::max(min - p, p - max), vec3(0.0f)));
return length2(tcnn::max(tcnn::max(min - p, p - max), vec3(0.0f)));
}

NGP_HOST_DEVICE float signed_distance(const vec3& p) const {
vec3 q = abs(p - min) - diag();
return length(glm::max(q, vec3(0.0f))) + std::min(compMax(q), 0.0f);
return length(tcnn::max(q, vec3(0.0f))) + std::min(tcnn::max(q), 0.0f);
}

NGP_HOST_DEVICE void get_vertices(vec3 v[8]) const {
Expand All @@ -249,12 +249,4 @@ struct BoundingBox {
vec3 max = vec3(-std::numeric_limits<float>::infinity());
};

inline std::ostream& operator<<(std::ostream& os, const ngp::BoundingBox& bb) {
os << "[";
os << "min=[" << bb.min.x << "," << bb.min.y << "," << bb.min.z << "], ";
os << "max=[" << bb.max.x << "," << bb.max.y << "," << bb.max.z << "]";
os << "]";
return os;
}

NGP_NAMESPACE_END
10 changes: 5 additions & 5 deletions include/neural-graphics-primitives/camera_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#pragma once

#include <neural-graphics-primitives/common.h>
#include <neural-graphics-primitives/common_host.h>

#include <tiny-cuda-nn/common.h>

Expand All @@ -28,7 +28,7 @@

struct ImDrawList;

NGP_NAMESPACE_BEGIN
namespace ngp {

struct CameraKeyframe {
quat R;
Expand All @@ -40,7 +40,7 @@ struct CameraKeyframe {
int glow_mode;
float glow_y_cutoff;
mat4x3 m() const {
auto rot = toMat3(normalize(quat(R)));
auto rot = to_mat3(normalize(quat(R)));
return mat4x3(rot[0], rot[1], rot[2], T);
}

Expand Down Expand Up @@ -113,7 +113,7 @@ struct CameraPath {
// add size to ensure no negative value is generated by modulo
return keyframes[(i + size) % size];
} else {
return keyframes[tcnn::clamp(i, 0, (int)keyframes.size()-1)];
return keyframes[clamp(i, 0, (int)keyframes.size()-1)];
}
}
CameraKeyframe eval_camera_path(float t) {
Expand Down Expand Up @@ -142,5 +142,5 @@ void visualize_cube(ImDrawList* list, const mat4& world2proj, const vec3& a, con
void visualize_nerf_camera(ImDrawList* list, const mat4& world2proj, const mat4x3& xform, float aspect, uint32_t col = 0x80ffffff, float thickness = 1.0f);
#endif

NGP_NAMESPACE_END
}

Loading

0 comments on commit 090aed6

Please sign in to comment.