Skip to content

Commit 426857c

Browse files
committed
Update
1 parent 9e76ed5 commit 426857c

2 files changed

Lines changed: 38 additions & 32 deletions

File tree

dlib/dnn/layers.h

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,19 +1017,10 @@ namespace dlib
10171017
void setup(const SUBNET& sub)
10181018
{
10191019
const auto& input = sub.get_output();
1020-
input_k = input.k();
1021-
input_nr = input.nr();
1022-
input_nc = input.nc();
1023-
1024-
// Calculate output dimensions using input dims where target is -1
1025-
if (k_ == -1) output_k = input_k;
1026-
if (nr_ == -1) output_nr = input_nr;
1027-
if (nc_ == -1) output_nc = input_nc;
1020+
update_dimensions_from_input(input);
10281021

1029-
// Check if this is well a pure reshape
10301022
long input_elements = input_k * input_nr * input_nc;
10311023
long output_elements = output_k * output_nr * output_nc;
1032-
if (input_elements != output_elements && input_k == output_k) needs_rescale = true;
10331024
DLIB_CASSERT(input_elements == output_elements || needs_rescale,
10341025
"Cannot reshape tensor of " << input_elements <<
10351026
" elements into shape with " << output_elements << " elements. " <<
@@ -1039,8 +1030,14 @@ namespace dlib
10391030
template <typename SUBNET>
10401031
void forward(const SUBNET& sub, resizable_tensor& output)
10411032
{
1042-
// Set the output size (always preserving batch dimension)
10431033
const tensor& input = sub.get_output();
1034+
1035+
// Check if dimensions changed (after deserialization or fine-tuning)
1036+
// This ensures dimensions are always synchronized with current input
1037+
if (input_k != input.k() || input_nr != input.nr() || input_nc != input.nc())
1038+
update_dimensions_from_input(input);
1039+
1040+
// Set the output size (always preserving batch dimension)
10441041
output.set_size(input.num_samples(), output_k, output_nr, output_nc);
10451042

10461043
if (!needs_rescale)
@@ -1142,7 +1139,25 @@ namespace dlib
11421139
<< "/>\n";
11431140
}
11441141

1145-
private:
1142+
private:
1143+
void update_dimensions_from_input(const tensor& input)
1144+
{
1145+
// Update input dimensions
1146+
input_k = input.k();
1147+
input_nr = input.nr();
1148+
input_nc = input.nc();
1149+
1150+
// Recalculate output dimensions for dynamic axes (-1)
1151+
if (k_ == -1) output_k = input_k;
1152+
if (nr_ == -1) output_nr = input_nr;
1153+
if (nc_ == -1) output_nc = input_nc;
1154+
1155+
// Check if rescaling is needed
1156+
long input_elements = input_k * input_nr * input_nc;
1157+
long output_elements = output_k * output_nr * output_nc;
1158+
needs_rescale = (input_elements != output_elements && input_k == output_k);
1159+
}
1160+
11461161
long input_k, input_nr, input_nc; // Input dimensions
11471162
long output_k, output_nr, output_nc; // Output dimensions
11481163
bool needs_rescale;

examples/slm_mixture_of_experts_ex.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ int main(int argc, char** argv)
592592
const long num_layers = 3;
593593
const long num_heads = 6;
594594
const long embedding_dim = 192;
595-
const long max_seq_len = 128;
595+
long max_seq_len = 128;
596596

597597
// Define transformer configuration with MoE
598598
using my_transformer = transformer_config<
@@ -933,21 +933,20 @@ int main(int argc, char** argv)
933933
cout << "Randomly selected segment #" << segment_idx << " (out of "
934934
<< tokenized_segments.size() << ") for generation\n";
935935
const auto& selected_segment = tokenized_segments[segment_idx];
936-
937-
long prompt_seq_len = max_seq_len;
938936
if (selected_segment.size() < (size_t)max_seq_len) {
939-
cerr << "Warning: Selected segment has only " << selected_segment.size()
937+
cerr << "Error: Selected segment has only " << selected_segment.size()
940938
<< " tokens, need at least " << max_seq_len << ".\n";
941-
prompt_seq_len = (selected_segment.size() * 2) / 3;
939+
//return 1;
940+
max_seq_len = (selected_segment.size() * 2) / 3;
942941
}
943942

944-
// Extract prompt tokens (first prompt_seq_len tokens of the segment)
943+
// Extract prompt tokens (first max_seq_len tokens of the segment)
945944
std::vector<int> prompt_tokens(selected_segment.begin(),
946-
selected_segment.begin() + prompt_seq_len);
945+
selected_segment.begin() + max_seq_len);
947946
cout << "Using " << prompt_tokens.size() << " tokens for initial prompt.\n";
948947

949948
// Setup inference context
950-
inference_context llm_context(max_seq_len*2, 4, tokenizer.get_special_token_id("<pad>"));
949+
inference_context llm_context(max_seq_len, 4, tokenizer.get_special_token_id("<pad>"));
951950
llm_context.add_tokens(prompt_tokens);
952951
auto input_seq = llm_context.get_input_window();
953952

@@ -966,7 +965,7 @@ int main(int argc, char** argv)
966965
cout << "Starting autoregressive generation...\n";
967966

968967
// Generation parameters
969-
const size_t tokens_to_generate = selected_segment.size() - prompt_seq_len;
968+
const size_t tokens_to_generate = selected_segment.size() - max_seq_len;
970969
std::vector<int> generated_tokens;
971970
generated_tokens.reserve(tokens_to_generate);
972971

@@ -1018,7 +1017,7 @@ int main(int argc, char** argv)
10181017
cout << "\n=== Validation: comparing generated vs. original segment ===\n";
10191018

10201019
// Extract reference tokens (the part we tried to regenerate)
1021-
std::vector<int> reference_tokens(selected_segment.begin() + prompt_seq_len,
1020+
std::vector<int> reference_tokens(selected_segment.begin() + max_seq_len,
10221021
selected_segment.end());
10231022

10241023
// Limit comparison to the length of generated tokens
@@ -1075,14 +1074,6 @@ int main(int argc, char** argv)
10751074
* randomization and augment_training_dataset() for noise injection. These techniques
10761075
* improve model robustness and generalization, enabling effective training on large
10771076
* volumes of information.
1078-
*
1079-
* - Transformer model configuration:
1080-
* + vocabulary size: 3500
1081-
* + layers: 4
1082-
* + attention heads: 6
1083-
* + embedding dimension: 228
1084-
* + max sequence length: 100
1085-
* - Number of parameters: 5,970,554 (training) - 5,432,738 (inference)
1086-
*
1087-
* After training, the model achieves excellent memorization of all internal datasets.
1077+
* After a complete training, the model achieves excellent memorization of all
1078+
* internal datasets.
10881079
*/

0 commit comments

Comments
 (0)