Skip to content

Commit

Permalink
debug matrix build
Browse files Browse the repository at this point in the history
  • Loading branch information
jkxing committed Apr 26, 2024
1 parent 09772c9 commit f3bbd06
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 189 deletions.
29 changes: 20 additions & 9 deletions src/base/differentiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ void Differentiation::materialize(CommandBuffer &command_buffer) noexcept {
auto length = buffer.size();
command_buffer << _param_buffer->subview(param_offset, length*8).copy_from(buffer.as<float>());
}

command_buffer << synchronize();

_optimizer->initialize(command_buffer, _param_buffer_size, *_param_buffer, *_param_grad_buffer, *_param_range_buffer);
}

Expand Down Expand Up @@ -382,6 +380,7 @@ void Differentiation::add_geom_gradients(Float3 grad_v, Float3 grad_n, Float3 we

auto instance = pipeline().geometry()->instance(inst_id);
auto triangle = pipeline().geometry()->triangle(instance, triangle_id);

_grad_buffer.value()->atomic(gradient_offset + triangle.i0 * 8 + 0).fetch_add(grad_v[0] * weight[0]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i0 * 8 + 1).fetch_add(grad_v[1] * weight[0]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i0 * 8 + 2).fetch_add(grad_v[2] * weight[0]);
Expand Down Expand Up @@ -495,10 +494,21 @@ void Differentiation::update_parameter_from_external(Stream &stream, luisa::vect
stream << buffer_view.copy_from(geoms[geoms_id[i]].view().as<Vertex>()) << synchronize() << p.mesh()->build() << synchronize();
_is_dirty = true;
}

//CommandBuffer command_buffer{&stream};
//pipeline().geometry()->build(command_buffer, _shapes, pipeline()._initial_time);
//stream << synchronize();
}

std::tuple<luisa::vector<void *>, luisa::vector<uint>> Differentiation::get_parameter_from_external(Stream &stream, luisa::vector<uint> &constants_id, luisa::vector<uint> &textures_id, luisa::vector<uint> &geoms_id) noexcept {
luisa::vector<void*> geom_param{};
luisa::vector<uint> geom_size{};
// apply geometry parameters
for (auto i: geoms_id) {
auto p = _geometry_params[i];
auto param_offset = p.param_offset();
auto buffer_view = p.buffer();
auto length = buffer_view.size();
geom_param.push_back(buffer_view.as<float>().native_handle());
geom_size.push_back(length*8u);
}
return std::make_tuple(geom_param, geom_size);
}

std::tuple<luisa::vector<void *>, luisa::vector<void *>> Differentiation::get_gradients(Stream &stream) {
Expand Down Expand Up @@ -527,10 +537,11 @@ std::tuple<luisa::vector<void *>, luisa::vector<void *>> Differentiation::get_gr
// apply geometry parameters
for (auto &&p : _geometry_params) {
auto param_offset = p.param_offset();
auto buffer_id = p.buffer_id();
auto [buffer_view, bindlessbuffer_id] = _pipeline.bindless_arena_buffer<Vertex>(buffer_id);
auto buffer_view = p.buffer();
auto length = buffer_view.size();
geom_res.push_back(_param_grad_buffer->subview(param_offset, length).as<float>().native_handle());
LUISA_INFO("here length is {}",length);
auto geom_grad_buf_view = _param_grad_buffer->subview(param_offset, length);
geom_res.push_back(reinterpret_cast<void*>(reinterpret_cast<uint64_t>(geom_grad_buf_view.native_handle())+geom_grad_buf_view.offset_bytes()));
}
return std::make_tuple(texture_res, geom_res);
}
Expand Down
4 changes: 4 additions & 0 deletions src/base/differentiation.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ class Differentiation {
void update_parameter_from_external(Stream &stream, luisa::vector<uint> &constants_id, luisa::vector<float4> &constants, luisa::vector<uint> &textures_id,
luisa::vector<Buffer<float4>> &textures, luisa::vector<uint> &geoms_id, luisa::vector<Buffer<float>> &geoms) noexcept;

std::tuple<luisa::vector<void *>, luisa::vector<uint>> get_parameter_from_external
(Stream &stream, luisa::vector<uint> &constants_id, luisa::vector<uint> &textures_id, luisa::vector<uint> &geoms_id) noexcept;


public:
[[nodiscard]] Float4 decode(const ConstantParameter &param) const noexcept;
void accumulate(const ConstantParameter &param, Expr<float4> grad, Expr<uint> slot_seed) const noexcept;
Expand Down
2 changes: 1 addition & 1 deletion src/base/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ luisa::shared_ptr<Interaction> Geometry::interaction(Expr<uint> inst_id, Expr<ui
auto tri = triangle(shape, prim_id);
auto attrib = shading_point(shape, tri, bary, m);
return luisa::make_shared<Interaction>(
std::move(shape), inst_id, prim_id,
std::move(shape), inst_id, prim_id, bary,
attrib, dot(wo, attrib.g.n) < 0.0f);
}

Expand Down
58 changes: 58 additions & 0 deletions src/integrators/bkp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// // mat->set(pixel_id_1d, id * 2 - 2 + j, 2 * id - 1, grad_uv_cur[0]);
// // mat->set(pixel_id_1d, id * 2 - 2 + j, 2 * id + 0, grad_uv_cur[1]);
// // mat->set(pixel_id_1d, id * 2 - 2 + j, 2 * id + 1, grad_uv_nxt[0]);
// // mat->set(pixel_id_1d, id * 2 - 2 + j, 2 * id + 2, grad_uv_nxt[1]);
// // $if(id>0){
// // };
// // auto point_pre_0_grad = grad(point_pre_0);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id - 1) + 0, point_pre_0_grad[0]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id - 1) + 1, point_pre_0_grad[1]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id - 1) + 2, point_pre_0_grad[2]);
// // auto point_pre_1_grad = grad(point_pre_1);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id - 1) + 3, point_pre_1_grad[0]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id - 1) + 4, point_pre_1_grad[1]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id - 1) + 5, point_pre_1_grad[2]);
// // auto point_pre_2_grad = grad(point_pre_2);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id - 1) + 6, point_pre_2_grad[0]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id - 1) + 7, point_pre_2_grad[1]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id - 1) + 8, point_pre_2_grad[2]);

// // auto point_nxt_0_grad = grad(point_nxt_0);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id + 1) + 0, point_nxt_0_grad[0]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id + 1) + 1, point_nxt_0_grad[1]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id + 1) + 2, point_nxt_0_grad[2]);
// // auto point_nxt_1_grad = grad(point_nxt_1);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id + 1) + 3, point_nxt_1_grad[0]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id + 1) + 4, point_nxt_1_grad[1]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id + 1) + 5, point_nxt_1_grad[2]);
// // auto point_nxt_2_grad = grad(point_nxt_2);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id + 1) + 6, point_nxt_2_grad[0]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id + 1) + 7, point_nxt_2_grad[1]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id + 1) + 8, point_nxt_2_grad[2]);

// // auto point_cur_0_grad = grad(point_cur_0);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 0, point_cur_0_grad[0]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 1, point_cur_0_grad[1]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 2, point_cur_0_grad[2]);
// // auto point_cur_1_grad = grad(point_cur_1);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 3, point_cur_1_grad[0]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 4, point_cur_1_grad[1]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 5, point_cur_1_grad[2]);
// // auto point_cur_2_grad = grad(point_cur_2);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 6, point_cur_2_grad[0]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 7, point_cur_2_grad[1]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 8, point_cur_2_grad[2]);

// // auto normal_cur_0_grad = grad(point_cur_0);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 9, normal_cur_0_grad[0]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 10, normal_cur_0_grad[1]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 11, normal_cur_0_grad[2]);
// // auto normal_cur_1_grad = grad(point_cur_1);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 12, normal_cur_1_grad[0]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 13, normal_cur_1_grad[1]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 14, normal_cur_1_grad[2]);
// // auto normal_cur_2_grad = grad(point_cur_2);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 15, normal_cur_2_grad[0]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 16, normal_cur_2_grad[1]);
// // mat_param->set(pixel_id_1d, id * 2 - 2 + j, 18 * (id) + 17, normal_cur_2_grad[2]);
// }
Loading

0 comments on commit f3bbd06

Please sign in to comment.