Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3e9b9f1
Implementation of linear_ layer for neural networks. This layer provi…
Cydral Apr 28, 2025
93ead3d
Minor change
Cydral May 2, 2025
bf1b805
Update dlib/dnn/layers.h
davisking May 3, 2025
49bfbc6
Merge branch 'davisking:master' into master
Cydral May 6, 2025
f234faa
Add reshape_to and flatten layers to Dlib's DNN module
Cydral May 6, 2025
26a2960
Missing update to "visitors.h"
Cydral May 22, 2025
c9a1ee4
format fixing for reshape_to
Cydral May 22, 2025
02e62d8
Update dlib/test/dnn.cpp
davisking May 23, 2025
394dee8
Merge branch 'davisking:master' into master
Cydral May 29, 2025
778bfc1
Vocabulary size fixed for learning, and function added for transforma…
Cydral May 29, 2025
03aafc2
Added a new example for learning a “complex” Transformer model.
Cydral May 29, 2025
22c2561
Added a new example for learning a “complex” Transformer model.
Cydral May 29, 2025
01cd0b2
Updated example for training a Transformer model.
Cydral May 29, 2025
6b63e55
fix for gcc/ffmpeg compilation
Cydral May 30, 2025
ad1f757
Fix a warning message for Ubuntu compilation.
Cydral May 30, 2025
c91c45a
Update for Linux environment.
Cydral May 30, 2025
6fcc0aa
Fix batch building
Cydral May 31, 2025
5a1773e
Slight improvement in model definition.
Cydral Jun 3, 2025
10d7c59
linear_ layer implementation improvement
Cydral Jun 7, 2025
d4bf94b
finalizing the example
Cydral Jun 7, 2025
a4dac0b
Fixing break condition in training method.
Cydral Jun 8, 2025
63454e3
Fixing declaration order of variables.
Cydral Jun 8, 2025
87ed70a
bpe_tokenizer improvements.
Cydral Jun 8, 2025
061c673
Example updated.
Cydral Jun 16, 2025
f6c8526
bpe_tokenizer class refactoring.
Cydral Jun 16, 2025
2db56f5
Example updated.
Cydral Jun 16, 2025
d4eeb2d
bpe_tokenizer class updated.
Cydral Jun 16, 2025
dcb5963
Decoding part of the bpe_tokenizer updated.
Cydral Jun 17, 2025
b81b502
Network definition update
Cydral Jun 27, 2025
80a6e0e
Merge branch 'davisking:master' into master
Cydral Aug 25, 2025
d520c2a
Add Adaptive Computation Time (ACT) layer with CPU/CUDA support
Cydral Aug 27, 2025
b089d58
Fixes
Cydral Aug 29, 2025
1a904f2
Update comments for params
Aldric-ATOS Sep 8, 2025
ab29fc4
Fixes and improvements
Cydral Sep 13, 2025
f16f743
Disabling enable_depth_scaling, which obviously affects the result of…
Cydral Sep 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions dlib/cuda/cpu_dlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3219,6 +3219,153 @@ namespace dlib

// ------------------------------------------------------------------------------------

void compute_act_halt_probabilities(
resizable_tensor& halt_probs,
resizable_tensor& logits,
const tensor& input_data,
const tensor& halt_params,
long batch_size,
long seq_len,
long feature_dim
)
{
const float* in_ptr = input_data.host();
const float* W_halt = halt_params.host();
const float b_halt = halt_params.host()[feature_dim];
float* logits_ptr = logits.host();
float* halt_probs_ptr = halt_probs.host();

const long d_model = feature_dim / input_data.k();
const long num_channels = input_data.k();

for (long pos = 0; pos < batch_size * seq_len; ++pos) {
const long n = pos / seq_len;
const long s = pos % seq_len;

float logit = b_halt;

for (long c = 0; c < num_channels; ++c) {
for (long d = 0; d < d_model; ++d) {
const long in_idx = ((n * num_channels + c) * seq_len + s) * d_model + d;
const long weight_idx = c * d_model + d;
logit += in_ptr[in_idx] * W_halt[weight_idx];
}
}

logits_ptr[pos] = logit;

halt_probs_ptr[pos] = 1.0f / (1.0f + std::exp(-logit));
}
}

void update_act_state(
resizable_tensor& output,
const tensor& input_data,
const tensor& halt_probs,
resizable_tensor& cumulative_halting,
resizable_tensor& remainders,
resizable_tensor& n_steps,
long batch_size,
long seq_len,
long d_model,
long num_channels,
float halt_threshold,
long current_step
)
{
const float* in_ptr = input_data.host();
const float* p_halt = halt_probs.host();
float* out_ptr = output.host();
float* cum_halt = cumulative_halting.host();
float* remain = remainders.host();
float* steps = n_steps.host();

for (long pos = 0; pos < batch_size * seq_len; ++pos) {
if (cum_halt[pos] < halt_threshold) {
const long n = pos / seq_len;
const long s = pos % seq_len;

float p = p_halt[pos];
float r = remain[pos];
float effective = std::min(p * r, halt_threshold - cum_halt[pos]);

cum_halt[pos] += effective;
remain[pos] -= effective;
steps[pos] = static_cast<float>(current_step + 1);

for (long c = 0; c < num_channels; ++c) {
for (long d = 0; d < d_model; ++d) {
const long idx = ((n * num_channels + c) * seq_len + s) * d_model + d;
out_ptr[idx] += effective * in_ptr[idx];
}
}
}
}
}

void finalize_act_output(
resizable_tensor& output,
const tensor& input_data,
const tensor& remainders,
long batch_size,
long seq_len,
long d_model,
long num_channels
)
{
const float* in_ptr = input_data.host();
const float* remain = remainders.host();
float* out_ptr = output.host();

for (long pos = 0; pos < batch_size * seq_len; ++pos) {
float r = remain[pos];
if (r > 1e-6f) {
const long n = pos / seq_len;
const long s = pos % seq_len;

for (long c = 0; c < num_channels; ++c) {
for (long d = 0; d < d_model; ++d) {
const long idx = ((n * num_channels + c) * seq_len + s) * d_model + d;
out_ptr[idx] += r * in_ptr[idx];
}
}
}
}
}

void apply_act_depth_scaling(
tensor& gradients,
const tensor& n_steps,
long batch_size,
long seq_len,
long d_model,
long num_channels,
float max_steps,
float scale_factor
)
{
const float* steps = n_steps.host();
float* grad_ptr = gradients.host();

for (long pos = 0; pos < batch_size * seq_len; ++pos)
{
const float scale = 1.0f + scale_factor * (steps[pos] / max_steps);
const long n = pos / seq_len;
const long s = pos % seq_len;

for (long c = 0; c < num_channels; ++c)
{
for (long d = 0; d < d_model; ++d)
{
const long idx = ((n * num_channels + c) * seq_len + s) * d_model + d;
grad_ptr[idx] *= scale;
}
}
}
}

// ------------------------------------------------------------------------------------

}
}

Expand Down
48 changes: 48 additions & 0 deletions dlib/cuda/cpu_dlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,54 @@ namespace dlib
bool scale
);

// -----------------------------------------------------------------------------------

void compute_act_halt_probabilities(
resizable_tensor& halt_probs,
resizable_tensor& logits,
const tensor& input_data,
const tensor& halt_params,
long batch_size,
long seq_len,
long feature_dim
);

void update_act_state(
resizable_tensor& output,
const tensor& input_data,
const tensor& halt_probs,
resizable_tensor& cumulative_halting,
resizable_tensor& remainders,
resizable_tensor& n_steps,
long batch_size,
long seq_len,
long d_model,
long num_channels,
float halt_threshold,
long current_step
);

void finalize_act_output(
resizable_tensor& output,
const tensor& input_data,
const tensor& remainders,
long batch_size,
long seq_len,
long d_model,
long num_channels
);

void apply_act_depth_scaling(
tensor& gradients,
const tensor& n_steps,
long batch_size,
long seq_len,
long d_model,
long num_channels,
float max_steps,
float scale_factor
);

// -----------------------------------------------------------------------------------

class pooling
Expand Down
Loading
Loading