Skip to content

Commit

Permalink
finish pipeline debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jkxing committed Apr 27, 2024
1 parent aee3643 commit 2d69442
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 58 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"

[project]
name = "luisarender-python"
version = "0.0.4"
version = "0.0.5"
description = "A High-Performance Rendering Framework with Layered and Unified Interfaces on Stream Architectures"
readme = "README.md"
authors = [
Expand Down
59 changes: 30 additions & 29 deletions src/base/differentiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,38 +386,39 @@ void Differentiation::add_geom_gradients(Float3 grad_v, Float3 grad_n, Float3 we
return;
};
UInt real_grad_offset = gradient_offset-1;
device_log("instance_id is {} grad {}",inst_id, real_grad_offset);
//device_log("instance_id is {} grad {}",inst_id, real_grad_offset);

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]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i0 * 8 + 3).fetch_add(grad_n[0] * weight[0]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i0 * 8 + 4).fetch_add(grad_n[1] * weight[0]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i0 * 8 + 5).fetch_add(grad_n[2] * weight[0]);
//device_log("triangle {} {} {}",triangle.i0, triangle.i1,triangle.i2);

_grad_buffer.value()->atomic(real_grad_offset + triangle.i0 * 8 + 0).fetch_add(grad_v[0] * weight[0]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i0 * 8 + 1).fetch_add(grad_v[1] * weight[0]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i0 * 8 + 2).fetch_add(grad_v[2] * weight[0]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i0 * 8 + 3).fetch_add(grad_n[0] * weight[0]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i0 * 8 + 4).fetch_add(grad_n[1] * weight[0]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i0 * 8 + 5).fetch_add(grad_n[2] * weight[0]);

_grad_buffer.value()->atomic(gradient_offset + triangle.i1 * 8 + 0).fetch_add(grad_v[0] * weight[1]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i1 * 8 + 1).fetch_add(grad_v[1] * weight[1]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i1 * 8 + 2).fetch_add(grad_v[2] * weight[1]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i1 * 8 + 3).fetch_add(grad_n[0] * weight[1]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i1 * 8 + 4).fetch_add(grad_n[1] * weight[1]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i1 * 8 + 5).fetch_add(grad_n[2] * weight[1]);

_grad_buffer.value()->atomic(gradient_offset + triangle.i2 * 8 + 0).fetch_add(grad_v[0] * weight[2]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i2 * 8 + 1).fetch_add(grad_v[1] * weight[2]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i2 * 8 + 2).fetch_add(grad_v[2] * weight[2]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i2 * 8 + 3).fetch_add(grad_n[0] * weight[2]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i2 * 8 + 4).fetch_add(grad_n[1] * weight[2]);
_grad_buffer.value()->atomic(gradient_offset + triangle.i2 * 8 + 5).fetch_add(grad_n[2] * weight[2]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i1 * 8 + 0).fetch_add(grad_v[0] * weight[1]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i1 * 8 + 1).fetch_add(grad_v[1] * weight[1]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i1 * 8 + 2).fetch_add(grad_v[2] * weight[1]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i1 * 8 + 3).fetch_add(grad_n[0] * weight[1]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i1 * 8 + 4).fetch_add(grad_n[1] * weight[1]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i1 * 8 + 5).fetch_add(grad_n[2] * weight[1]);

_grad_buffer.value()->atomic(real_grad_offset + triangle.i2 * 8 + 0).fetch_add(grad_v[0] * weight[2]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i2 * 8 + 1).fetch_add(grad_v[1] * weight[2]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i2 * 8 + 2).fetch_add(grad_v[2] * weight[2]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i2 * 8 + 3).fetch_add(grad_n[0] * weight[2]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i2 * 8 + 4).fetch_add(grad_n[1] * weight[2]);
_grad_buffer.value()->atomic(real_grad_offset + triangle.i2 * 8 + 5).fetch_add(grad_n[2] * weight[2]);

for(uint i=0;i<6;i++)
{
_counter.value()->atomic(gradient_offset + triangle.i0 * 8 + i).fetch_add(UInt(1));
_counter.value()->atomic(gradient_offset + triangle.i1 * 8 + i).fetch_add(UInt(1));
_counter.value()->atomic(gradient_offset + triangle.i2 * 8 + i).fetch_add(UInt(1));
}
// for(uint i=0;i<6;i++)
// {
// _counter.value()->atomic(real_grad_offset + triangle.i0 * 8 + i).fetch_add(UInt(1));
// _counter.value()->atomic(real_grad_offset + triangle.i1 * 8 + i).fetch_add(UInt(1));
// _counter.value()->atomic(real_grad_offset + triangle.i2 * 8 + i).fetch_add(UInt(1));
// }
}

void Differentiation::step(CommandBuffer &command_buffer) noexcept {
Expand Down Expand Up @@ -548,11 +549,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 grad_offset = p.gradient_buffer_offset();
auto buffer_view = p.buffer();
auto length = buffer_view.size();
LUISA_INFO("here length is {}",length);
auto geom_grad_buf_view = _param_grad_buffer->subview(param_offset, length);
auto geom_grad_buf_view = _grad_buffer->subview(grad_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
52 changes: 25 additions & 27 deletions src/integrators/megappm_diff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1361,10 +1361,9 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
//path_size = depth;
auto wi = -ray->direction();
auto it = pipeline().geometry()->intersect(ray);

// miss
$if(!it->valid()) {
$if(photon_id_1d<1)
$if(photon_id_1d==node<MegakernelPhotonMappingDiff>()->debug_photon())
{
device_log("break it unvalid at size {} id {}", path_size, photon_id_1d);
device_log("ray {} {}", ray->origin(), ray->direction());
Expand All @@ -1373,14 +1372,12 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
};

$if(!it->shape().has_surface()) {
$if(photon_id_1d<1)
$if(photon_id_1d==node<MegakernelPhotonMappingDiff>()->debug_photon())
{
device_log("break it non surface {} ", photon_id_1d);
};
$break;
};


// generate uniform samples
auto u_lobe = sampler()->generate_1d();
auto u_bsdf = sampler()->generate_2d();
Expand Down Expand Up @@ -1444,7 +1441,6 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
auto grad_b = grad(bary).xy();
grad_bary += grad_b;
grad_beta += grad(beta_diff);

};
count_neighbors+=1;
};
Expand Down Expand Up @@ -1488,7 +1484,7 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
});
beta = zero_if_any_nan(beta);
$if(beta.all([](auto b) noexcept { return b <= 0.f; })) {
$if(photon_id_1d<1)
$if(photon_id_1d==node<MegakernelPhotonMappingDiff>()->debug_photon())
{
device_log("break beta negative {}",photon_id_1d);
};
Expand All @@ -1498,7 +1494,7 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
auto q = max(eta_scale, .05f);
$if(depth + 1u >= rr_depth) {
$if(q < rr_threshold & u_rr >= q) {
$if(photon_id_1d<1)
$if(photon_id_1d==node<MegakernelPhotonMappingDiff>()->debug_photon())
{
device_log("break rr {}",photon_id_1d);
};
Expand All @@ -1511,20 +1507,19 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
bary_coords[path_size] = it->bary_coord();
points[path_size] = it->p();
normals[path_size] = it->ng();
// $if(photon_id_1d<1)
// $if(photon_id_1d==node<MegakernelPhotonMappingDiff>()->debug_photon())
// {
// device_log("adding path size {} id is {}",path_size,photon_id_1d);
// };
path_size = path_size+1u;
};
$if(photon_id_1d<1){
device_log("path size is {} id is {}",path_size,photon_id_1d);
$for(i,path_size)
{
device_log("{} point {} normal {} eta {}, bary {}",i, points[i], normals[i], etas[i], bary_coords[i]);
};
EPSM_photon(path_size, points, normals, inst_ids, triangle_ids, bary_coords, etas, light_sample, grad_betas, grad_barys, mat_bary, mat_param);
};
// $if(photon_id_1d==node<MegakernelPhotonMappingDiff>()->debug_photon()){
// device_log("path size is {} id is {}",path_size,photon_id_1d);
// $for(i,path_size)
// {
// device_log("{} point {} normal {} eta {}, bary {}",i, points[i], normals[i], etas[i], bary_coords[i]);
// };
// };
// $if(path_size>=2u)
// {
// device_log("path size is {}",path_size);
Expand All @@ -1536,20 +1531,16 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
// return;
// EPSM_photon(path_size, points, normals, inst_ids, triangle_ids, bary_coords, etas, light_sample, grad_betas, grad_barys, mat_bary, mat_param);
// };
$if(tot_neighbors>0)
$if(photon_id_1d<node<MegakernelPhotonMappingDiff>()->debug_photon())
{

$if(path_size>=2&path_size<=4) {
EPSM_photon(path_size, points, normals, inst_ids, triangle_ids, bary_coords, etas, light_sample, grad_betas, grad_barys, mat_bary, mat_param);
};
};
}
void EPSM_photon(UInt path_size, ArrayFloat3<4> &points, ArrayFloat3<4> &normals, ArrayUInt<4> &inst_ids, ArrayUInt<4> &triangle_ids, ArrayFloat3<4> &bary_coords,
ArrayFloat<4> &etas, LightSampler::Sample &light_sample, ArrayFloat3<4> grad_beta, ArrayFloat2<4> grad_bary, ArrayFloat<8 * 8 * 2> &mat_bary, ArrayFloat3<8 * 4> &mat_param){
{
$if(path_size<2|path_size>4) {
return;
};
// Shared<float> *mat_bary = new Shared<float>(16*16);
// Shared<float3> *mat_param = new Shared<float3>(32);

auto locate = [&](UInt i, UInt j) {
return (i<<4|j);
};
Expand Down Expand Up @@ -1618,8 +1609,11 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
tmp[i] -= grad_bary[j][0] * mat_bary[locate_adj(j * 2, i)] + grad_bary[j][1] * mat_bary[locate_adj(j * 2 + 1, i)];
};
};
$for (i,8) {
device_log("mat_param {} {} {} {}",mat_param[i*4+0],mat_param[i*4+1],mat_param[i*4+2],mat_param[i*4+3]);
$if(node<MegakernelPhotonMappingDiff>()->debug())
{
$for (i,8) {
device_log("mat_param {} {} {} {}",mat_param[i*4+0],mat_param[i*4+1],mat_param[i*4+2],mat_param[i*4+3]);
};
};
$for(i, n/2) {
Float3 grad_vertex = make_float3(0.0f), grad_normal = make_float3(0.0f);
Expand All @@ -1631,6 +1625,10 @@ class MegakernelPhotonMappingDiffInstance : public DifferentiableIntegrator::Ins
$if(i<n/2-2){
grad_vertex += tmp[i*2+2]*mat_param[((i*2+2)<<2)] + tmp[i*2+3]*mat_param[((i*2+3)<<2)];
};
$if(node<MegakernelPhotonMappingDiff>()->debug())
{
device_log("grad_vertex {} grad_normal {}",grad_vertex, grad_normal);
};
$if(inst_ids[i]==0)
{
pipeline().differentiation()->add_geom_gradients(grad_vertex, grad_normal, bary_coords[i], inst_ids[i], triangle_ids[i]);
Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_ad_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def torch_ensure_grad_shape(a, b):
tex_grad, geom_grad = luisarender.get_gradients()
geom_grad_torch = cu_device_ptr_to_torch_tensor(geom_grad[0], vertex_pos.shape, dtype=cupy.float32)
print(loss, torch.max(geom_grad_torch), torch.min(geom_grad_torch), geom_grad_torch.shape)
exit()
#exit()
luisarender.update_scene([x])

#exit()
Expand Down

0 comments on commit 2d69442

Please sign in to comment.