File size: 52,039 Bytes
1e95f87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
% LexiMind: A Hybrid Transformer Architecture for Multi-Task NLP
% IEEE Conference Style Paper
% Author: Oliver Perrin

\documentclass[conference]{IEEEtran}
\IEEEoverridecommandlockouts

% Essential packages
\usepackage{cite}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{algorithmic}
\usepackage{graphicx}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{array}

% TikZ for diagrams
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, positioning, fit, calc, backgrounds, decorations.pathreplacing}

% Code listings style
\lstset{
    basicstyle=\ttfamily\small,
    breaklines=true,
    frame=single,
    language=Python,
    keywordstyle=\color{blue},
    commentstyle=\color{green!50!black},
    stringstyle=\color{red!60!black},
    showstringspaces=false
}

% Hyperref setup
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    citecolor=blue,
    urlcolor=blue
}

\def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em
    T\kern-.1667em\lower.7ex\hbox{E}\kern-.125emX}}

\begin{document}

\title{LexiMind: A Hybrid Transformer Architecture\\for Multi-Task Natural Language Processing}

\author{\IEEEauthorblockN{Oliver Perrin}
\IEEEauthorblockA{Department of Computer Science\\
Appalachian State University\\
Bachelor of Science in Computer Science\\
Email: perrinob@appstate.edu}}

\maketitle

\begin{abstract}
This paper presents LexiMind, a multi-task Natural Language Processing (NLP) system that combines a custom-built Transformer architecture with pre-trained weights from Google's FLAN-T5 (Fine-tuned Language Net Text-to-Text Transfer Transformer). The system performs three fundamental NLP tasks simultaneously: abstractive text summarization, multi-label emotion classification, and single-label topic classification. Unlike news-focused models, LexiMind specializes in literary and academic content, trained on Goodreads book descriptions matched with Project Gutenberg texts, arXiv academic paper abstracts, and GoEmotions for emotion classification. By implementing modern architectural innovations including Pre-Layer Normalization (Pre-LN) with Root Mean Square Layer Normalization (RMSNorm), T5-style relative position bias, FlashAttention via PyTorch 2.0's Scaled Dot-Product Attention (SDPA), gradient checkpointing, and torch.compile optimization, LexiMind achieves efficient training on consumer GPUs while maintaining strong performance. Our final model achieves a BERTScore F1 of 0.83 for summarization, 85.2\% accuracy for topic classification, and competitive multi-label F1 for emotion detection. The 272M-parameter architecture is constructed from first principles in a bottom-up fashion, with each component (attention mechanisms, feed-forward networks, encoder/decoder blocks) implemented as standalone modules. A factory pattern enables seamless weight transfer from FLAN-T5-base, allowing the system to leverage Google's pre-trained knowledge while maintaining full architectural transparency and customization capability.
\end{abstract}

\begin{IEEEkeywords}
Transformer, Multi-Task Learning, Natural Language Processing, FLAN-T5, Transfer Learning, Text Summarization, Emotion Classification, Academic Papers, Literary Text
\end{IEEEkeywords}

%=============================================================================
\section{Introduction}
%=============================================================================

The Transformer architecture \cite{vaswani2017attention} has fundamentally reshaped Natural Language Processing (NLP), establishing itself as the foundation for state-of-the-art models across virtually all language understanding and generation tasks. Building upon this foundation, the T5 (Text-to-Text Transfer Transformer) model \cite{raffel2020exploring} introduced a unified framework that casts all NLP problems as text-to-text transformations. FLAN-T5 (Fine-tuned Language Net) \cite{chung2022scaling} further enhanced T5's capabilities through instruction fine-tuning on over 1,000 diverse tasks.

While pre-trained models like FLAN-T5 offer impressive zero-shot and few-shot capabilities, they are often treated as black boxes—their internal mechanisms obscured by framework abstractions. This opacity hinders both understanding and customization. Furthermore, multi-task learning scenarios often require architectural modifications that pre-built models do not easily accommodate.

LexiMind addresses these challenges through a hybrid approach: implementing a complete Transformer architecture from scratch while maintaining compatibility with FLAN-T5's pre-trained weights. This provides several key advantages:

\begin{enumerate}
    \item \textbf{Architectural Transparency}: Every component—from attention mechanisms to normalization layers—is explicitly implemented and documented.
    \item \textbf{Customization Flexibility}: Task-specific heads and routing logic can be freely modified without framework constraints.
    \item \textbf{Transfer Learning}: FLAN-T5's linguistic knowledge is transferred through careful weight mapping in the factory module.
    \item \textbf{Modern Optimizations}: Integration of FlashAttention, bfloat16 training, and gradient accumulation ensures efficient resource utilization.
\end{enumerate}

The contributions of this work include:
\begin{itemize}
    \item A custom Transformer implementation compatible with T5/FLAN-T5 weight loading
    \item A multi-task architecture supporting both generative (summarization) and discriminative (classification) tasks
    \item Detailed documentation of weight transfer mechanisms between pre-trained models and custom implementations
    \item Comprehensive training infrastructure with mixed-precision support, gradient monitoring, and MLflow experiment tracking
\end{itemize}

%=============================================================================
\section{Related Work}
%=============================================================================

\subsection{Transformer Architectures}

The original Transformer \cite{vaswani2017attention} introduced the self-attention mechanism, enabling parallel processing of sequences and effective capture of long-range dependencies. The architecture consists of stacked encoder and decoder blocks, each containing Multi-Head Attention (MHA) and position-wise Feed-Forward Networks (FFN).

\textbf{Layer Normalization Placement}: The original Transformer applied Layer Normalization \cite{ba2016layer} after residual connections (Post-LN). Subsequent research \cite{xiong2020layer} demonstrated that applying normalization before sublayers (Pre-LN) significantly improves training stability, particularly for deep networks. LexiMind adopts the Pre-LN configuration used by T5 and modern large language models.

\textbf{RMSNorm}: Zhang and Sennrich \cite{zhang2019root} proposed Root Mean Square Layer Normalization (RMSNorm), which removes the mean-centering operation of standard LayerNorm while maintaining comparable performance. T5 \cite{raffel2020exploring} adopts this approach, and LexiMind follows suit for compatibility.

\subsection{Pre-trained Language Models}

\textbf{T5}: Raffel et al. \cite{raffel2020exploring} introduced the T5 model, which frames all NLP tasks as text-to-text problems. T5 uses a Transformer encoder-decoder architecture with several distinctive features: relative position bias instead of absolute positional embeddings, RMSNorm for layer normalization, and a gated feed-forward network.

\textbf{FLAN-T5}: Chung et al. \cite{chung2022scaling} enhanced T5 through instruction fine-tuning, creating FLAN-T5. By training on diverse task instructions, FLAN-T5 demonstrates improved zero-shot and few-shot capabilities compared to the original T5.

\subsection{Multi-Task Learning}

Multi-Task Learning (MTL) \cite{caruana1997multitask} trains a single model on multiple related tasks, promoting parameter sharing and implicit data augmentation. Hard parameter sharing—where lower layers are shared across tasks while task-specific heads branch from shared representations—remains the dominant approach for Transformer-based MTL systems.

%=============================================================================
\section{Architecture}
%=============================================================================

LexiMind implements a complete encoder-decoder Transformer with task-specific heads, constructed using a bottom-up approach where each component is implemented as a standalone module. Figure \ref{fig:architecture} illustrates the high-level system architecture.

\begin{figure}[htbp]
\centering
\begin{tikzpicture}[
    scale=0.75,
    transform shape,
    box/.style={draw, rectangle, minimum width=2cm, minimum height=0.7cm, align=center, rounded corners=2pt},
    smallbox/.style={draw, rectangle, minimum width=1.4cm, minimum height=0.5cm, align=center, rounded corners=2pt, font=\scriptsize},
    head/.style={draw, rectangle, minimum width=1.5cm, minimum height=0.6cm, align=center, rounded corners=2pt, fill=blue!20},
    arrow/.style={->, >=stealth, thick},
    dashedarrow/.style={->, >=stealth, dashed}
]

% Input
\node[box, fill=gray!20] (input) at (0, 0) {Input Text};

% Tokenizer
\node[box, fill=yellow!30] (tokenizer) at (0, 1.2) {Tokenizer\\(SentencePiece)};

% Encoder
\node[box, fill=green!30, minimum height=2cm] (encoder) at (0, 3.2) {Encoder\\$N=12$ layers};

% Task routing
\node[box, fill=orange!30] (router) at (0, 5.2) {Task Router};

% Decoder branch
\node[box, fill=green!30, minimum height=1.5cm] (decoder) at (-2.5, 7) {Decoder\\$N=12$ layers};
\node[head] (lmhead) at (-2.5, 8.8) {LM Head};
\node[smallbox, fill=purple!20] (summ) at (-2.5, 9.8) {Summary};

% Classification branch
\node[head] (emotionhead) at (1.2, 7) {Emotion\\Head};
\node[head] (topichead) at (2.8, 7) {Topic\\Head};
\node[smallbox, fill=purple!20] (emotion) at (1.2, 8.2) {Emotions\\(28 classes)};
\node[smallbox, fill=purple!20] (topic) at (2.8, 8.2) {Topics\\(7 classes)};

% Arrows
\draw[arrow] (input) -- (tokenizer);
\draw[arrow] (tokenizer) -- (encoder);
\draw[arrow] (encoder) -- (router);
\draw[arrow] (router) -- (decoder);
\draw[arrow] (router) -- (emotionhead);
\draw[arrow] (router) -- (topichead);
\draw[arrow] (decoder) -- (lmhead);
\draw[arrow] (lmhead) -- (summ);
\draw[arrow] (emotionhead) -- (emotion);
\draw[arrow] (topichead) -- (topic);

% Cross-attention arrow
\draw[dashedarrow] (encoder.west) -- ++(-0.5,0) |- (decoder.south west);

% Labels
\node[font=\tiny, align=center] at (-1.8, 4.5) {Cross\\Attention};

\end{tikzpicture}
\caption{LexiMind system architecture showing the shared encoder, task-specific routing, decoder for generation, and classification heads for discriminative tasks.}
\label{fig:architecture}
\end{figure}

\subsection{Transformer Block Structure}

Figure \ref{fig:transformer_block} presents the internal structure of encoder and decoder blocks, following the Pre-LN configuration from T5 \cite{raffel2020exploring}.

\begin{figure}[htbp]
\centering
\begin{tikzpicture}[
    scale=0.65,
    transform shape,
    block/.style={draw, rectangle, minimum width=2.5cm, minimum height=0.6cm, align=center, rounded corners=2pt},
    norm/.style={draw, rectangle, minimum width=2.5cm, minimum height=0.5cm, align=center, fill=yellow!30, rounded corners=2pt},
    attn/.style={draw, rectangle, minimum width=2.5cm, minimum height=0.6cm, align=center, fill=blue!25, rounded corners=2pt},
    ffn/.style={draw, rectangle, minimum width=2.5cm, minimum height=0.6cm, align=center, fill=green!25, rounded corners=2pt},
    add/.style={draw, circle, minimum size=0.4cm, fill=red!20, inner sep=0pt, font=\small},
    arrow/.style={->, >=stealth},
]

% === ENCODER BLOCK ===
\node[font=\bfseries] at (0, 8) {Encoder Block};

% Input
\node (enc_in) at (0, 7) {};
\draw[arrow] (0, 6.5) -- (enc_in);

% RMSNorm 1
\node[norm] (enc_norm1) at (0, 6) {RMSNorm};

% Self-Attention
\node[attn] (enc_attn) at (0, 5) {Multi-Head\\Self-Attention};

% Add 1
\node[add] (enc_add1) at (0, 4) {+};

% RMSNorm 2
\node[norm] (enc_norm2) at (0, 3) {RMSNorm};

% FFN
\node[ffn] (enc_ffn) at (0, 2) {Gated FFN\\(GELU)};

% Add 2
\node[add] (enc_add2) at (0, 1) {+};

% Output
\node (enc_out) at (0, 0.3) {};

% Connections
\draw[arrow] (enc_in) -- (enc_norm1);
\draw[arrow] (enc_norm1) -- (enc_attn);
\draw[arrow] (enc_attn) -- (enc_add1);
\draw[arrow] (enc_add1) -- (enc_norm2);
\draw[arrow] (enc_norm2) -- (enc_ffn);
\draw[arrow] (enc_ffn) -- (enc_add2);
\draw[arrow] (enc_add2) -- (enc_out);

% Residual connections
\draw[arrow] (0, 6.5) -- (-1.5, 6.5) -- (-1.5, 4) -- (enc_add1.west);
\draw[arrow] (enc_add1.east) -- (1.5, 4) -- (1.5, 1) -- (enc_add2.east);

% === DECODER BLOCK ===
\node[font=\bfseries] at (5.5, 8) {Decoder Block};

% Input
\node (dec_in) at (5.5, 7) {};
\draw[arrow] (5.5, 6.5) -- (dec_in);

% RMSNorm 1
\node[norm] (dec_norm1) at (5.5, 6) {RMSNorm};

% Masked Self-Attention
\node[attn] (dec_attn1) at (5.5, 5) {Masked\\Self-Attention};

% Add 1
\node[add] (dec_add1) at (5.5, 4.2) {+};

% RMSNorm 2
\node[norm] (dec_norm2) at (5.5, 3.4) {RMSNorm};

% Cross-Attention
\node[attn, fill=cyan!25] (dec_attn2) at (5.5, 2.4) {Cross-Attention};

% Add 2
\node[add] (dec_add2) at (5.5, 1.5) {+};

% RMSNorm 3
\node[norm] (dec_norm3) at (5.5, 0.7) {RMSNorm};

% FFN
\node[ffn] (dec_ffn) at (5.5, -0.3) {Gated FFN\\(GELU)};

% Add 3
\node[add] (dec_add3) at (5.5, -1.2) {+};

% Connections
\draw[arrow] (dec_in) -- (dec_norm1);
\draw[arrow] (dec_norm1) -- (dec_attn1);
\draw[arrow] (dec_attn1) -- (dec_add1);
\draw[arrow] (dec_add1) -- (dec_norm2);
\draw[arrow] (dec_norm2) -- (dec_attn2);
\draw[arrow] (dec_attn2) -- (dec_add2);
\draw[arrow] (dec_add2) -- (dec_norm3);
\draw[arrow] (dec_norm3) -- (dec_ffn);
\draw[arrow] (dec_ffn) -- (dec_add3);

% Encoder memory input
\node[block, fill=gray!20, minimum width=1.2cm, font=\scriptsize] (memory) at (8, 2.4) {Encoder\\Memory};
\draw[arrow] (memory) -- (dec_attn2);

% Residual connections (simplified)
\draw[arrow] (5.5, 6.5) -- (4, 6.5) -- (4, 4.2) -- (dec_add1.west);
\draw[arrow] (dec_add1.east) -- (7, 4.2) -- (7, 1.5) -- (dec_add2.east);
\draw[arrow] (dec_add2.west) -- (4, 1.5) -- (4, -1.2) -- (dec_add3.west);

\end{tikzpicture}
\caption{Pre-LN Transformer blocks. Left: Encoder block with self-attention and FFN. Right: Decoder block with masked self-attention, cross-attention to encoder memory, and FFN. RMSNorm is applied \emph{before} each sublayer (Pre-LN).}
\label{fig:transformer_block}
\end{figure}

\subsection{Multi-Head Attention Mechanism}

The attention mechanism is the cornerstone of the Transformer architecture. LexiMind implements Multi-Head Attention with support for T5-style relative position bias and FlashAttention optimization. Figure \ref{fig:attention} illustrates the attention computation.

\begin{figure}[htbp]
\centering
\begin{tikzpicture}[
    scale=0.6,
    transform shape,
    box/.style={draw, rectangle, minimum width=1.5cm, minimum height=0.6cm, align=center, rounded corners=2pt},
    proj/.style={draw, rectangle, minimum width=1.2cm, minimum height=0.5cm, align=center, fill=blue!20, rounded corners=2pt, font=\scriptsize},
    op/.style={draw, rectangle, minimum width=1.2cm, minimum height=0.5cm, align=center, fill=orange!30, rounded corners=2pt, font=\scriptsize},
    arrow/.style={->, >=stealth},
]

% Input
\node[box, fill=gray!20] (input) at (0, 0) {Input $X$};

% Projections
\node[proj] (wq) at (-2.5, 1.5) {$W_Q$};
\node[proj] (wk) at (0, 1.5) {$W_K$};
\node[proj] (wv) at (2.5, 1.5) {$W_V$};

% Q, K, V
\node[box, fill=green!20] (q) at (-2.5, 2.8) {$Q$};
\node[box, fill=green!20] (k) at (0, 2.8) {$K$};
\node[box, fill=green!20] (v) at (2.5, 2.8) {$V$};

% Split heads
\node[op] (split) at (0, 4) {Split $h$ heads};

% Attention scores
\node[op] (matmul1) at (0, 5.2) {$QK^T$};

% Position bias
\node[box, fill=yellow!30, font=\scriptsize] (bias) at (3.5, 5.2) {Relative\\Pos Bias};

% Add bias
\node[op] (add) at (0, 6.2) {$+ B_{rel}$};

% Scale (optional)
\node[op] (scale) at (0, 7.2) {Scale / Mask};

% Softmax
\node[op, fill=red!20] (softmax) at (0, 8.2) {Softmax};

% MatMul with V
\node[op] (matmul2) at (0, 9.2) {$\times V$};

% Concat
\node[op] (concat) at (0, 10.2) {Concat heads};

% Output projection
\node[proj] (wo) at (0, 11.2) {$W_O$};

% Output
\node[box, fill=purple!20] (output) at (0, 12.2) {Output};

% Arrows
\draw[arrow] (input) -- (wq);
\draw[arrow] (input) -- (wk);
\draw[arrow] (input) -- (wv);
\draw[arrow] (wq) -- (q);
\draw[arrow] (wk) -- (k);
\draw[arrow] (wv) -- (v);
\draw[arrow] (q) -- (split);
\draw[arrow] (k) -- (split);
\draw[arrow] (v.north) -- ++(0, 0.3) -| (2.5, 9.2) -- (matmul2);
\draw[arrow] (split) -- (matmul1);
\draw[arrow] (matmul1) -- (add);
\draw[arrow] (bias) -- (add);
\draw[arrow] (add) -- (scale);
\draw[arrow] (scale) -- (softmax);
\draw[arrow] (softmax) -- (matmul2);
\draw[arrow] (matmul2) -- (concat);
\draw[arrow] (concat) -- (wo);
\draw[arrow] (wo) -- (output);

% Annotations
\node[font=\tiny, align=left] at (-4.5, 5.5) {T5 does NOT\\scale by $\sqrt{d_k}$};

\end{tikzpicture}
\caption{Multi-Head Attention with T5-style relative position bias. The attention scores are computed as $QK^T + B_{rel}$, where $B_{rel}$ is the learned relative position bias. Unlike standard Transformers, T5 does not scale by $\sqrt{d_k}$.}
\label{fig:attention}
\end{figure}

The attention computation in LexiMind is implemented in \texttt{src/models/attention.py}. For T5 compatibility, the \texttt{scale\_scores} parameter controls whether to apply $\sqrt{d_k}$ scaling—T5 does not use this scaling \cite{raffel2020exploring}.

\subsubsection{T5 Relative Position Bias}

Unlike absolute positional embeddings that are added to token embeddings, T5 uses relative position bias added directly to attention scores. The \texttt{T5RelativePositionBias} class implements logarithmically-bucketed relative positions:

\begin{equation}
B_{ij} = \text{Embed}[\text{bucket}(i - j)]
\end{equation}

where $\text{bucket}(\cdot)$ maps relative distances to discrete buckets. Half the buckets encode exact positions for nearby tokens; the remaining half use logarithmic spacing for distant tokens. As documented in the code:

\begin{quote}
\emph{``T5 uses a combination of exact positions (for nearby tokens) and logarithmically-spaced buckets (for distant tokens).''} — \texttt{attention.py}, lines 46--48
\end{quote}

\subsubsection{FlashAttention Integration}

LexiMind leverages PyTorch 2.0's \texttt{scaled\_dot\_product\_attention} function, which automatically selects the optimal attention kernel:

\begin{quote}
\emph{``Uses F.scaled\_dot\_product\_attention which automatically selects the best available kernel (FlashAttention v2, Memory-Efficient Attention, or math fallback) based on hardware and input shapes.''} — \texttt{attention.py}, lines 134--137
\end{quote}

This provides O(N) memory complexity instead of O(N²) when FlashAttention is available.

\subsection{Feed-Forward Network}

Following T5, LexiMind implements a gated feed-forward network with GELU activation:

\begin{equation}
\text{FFN}(x) = (\text{GELU}(xW_g) \odot xW_1) W_2
\end{equation}

where $W_g$ is the gating projection, $W_1$ is the up-projection, $W_2$ is the down-projection, and $\odot$ denotes element-wise multiplication.

\subsection{RMSNorm}

RMSNorm \cite{zhang2019root} normalizes inputs using only the root mean square:

\begin{equation}
\text{RMSNorm}(x) = \frac{x}{\sqrt{\frac{1}{d}\sum_{i=1}^{d}x_i^2 + \epsilon}} \cdot \gamma
\end{equation}

The implementation in \texttt{src/models/t5\_layer\_norm.py} follows T5's convention, using only a learned scale parameter $\gamma$ with no bias term.

%=============================================================================
\section{Tokenization}
\label{sec:tokenization}
%=============================================================================

LexiMind wraps HuggingFace's AutoTokenizer with a simplified façade that handles T5-specific conventions. The implementation in \texttt{src/data/tokenization.py} manages special token handling and decoder input preparation.

\subsection{T5 Tokenizer Characteristics}

T5 uses SentencePiece \cite{kudo2018sentencepiece} with unigram tokenization:

\begin{itemize}
    \item \textbf{Vocabulary Size}: 32,128 tokens (padded to multiple of 128 for efficiency)
    \item \textbf{Special Tokens}: \texttt{pad\_token\_id=0}, \texttt{eos\_token\_id=1}
    \item \textbf{No Explicit BOS}: T5 uses the pad token as the decoder start token
\end{itemize}

As noted in the tokenizer implementation:

\begin{quote}
\emph{``T5 uses different special tokens than BART: T5: pad=0, eos=1, no explicit bos (uses pad or eos as decoder start); BART: bos=0, pad=1, eos=2.''} — \texttt{tokenization.py}, lines 42--44
\end{quote}

\subsection{Decoder Input Preparation}

For seq2seq training, decoder inputs must be shifted right from labels. The \texttt{prepare\_decoder\_inputs} method handles this:

\begin{lstlisting}[caption={Decoder input preparation from tokenization.py}]
def prepare_decoder_inputs(
    self, labels: torch.Tensor
) -> torch.Tensor:
    """Shift decoder labels to create 
    input ids prefixed by BOS."""
    bos = self.bos_token_id
    pad = self.pad_token_id
    decoder_inputs = torch.full_like(labels, pad)
    decoder_inputs[:, 0] = bos
    decoder_inputs[:, 1:] = labels[:, :-1]
    return decoder_inputs
\end{lstlisting}

%=============================================================================
\section{The Factory Module: Weight Transfer from FLAN-T5}
\label{sec:factory}
%=============================================================================

The \texttt{factory.py} module is central to LexiMind's hybrid approach, providing model construction and weight loading utilities. Figure \ref{fig:factory_flow} illustrates the model construction pipeline.

\begin{figure}[htbp]
\centering
\begin{tikzpicture}[
    scale=0.7,
    transform shape,
    box/.style={draw, rectangle, minimum width=2.5cm, minimum height=0.7cm, align=center, rounded corners=3pt},
    config/.style={draw, rectangle, minimum width=2cm, minimum height=0.6cm, align=center, fill=yellow!30, rounded corners=2pt, font=\small},
    model/.style={draw, rectangle, minimum width=2.2cm, minimum height=0.6cm, align=center, fill=green!30, rounded corners=2pt, font=\small},
    arrow/.style={->, >=stealth, thick},
]

% Config loading
\node[config] (yaml) at (0, 0) {config.yaml};
\node[box, fill=blue!20] (loadconfig) at (0, 1.3) {load\_model\_config()};
\node[config] (modelconfig) at (0, 2.6) {ModelConfig};

% Model building
\node[box, fill=blue!20] (build) at (0, 4.2) {build\_multitask\_model()};

% Components
\node[model] (encoder) at (-2.5, 5.8) {Encoder};
\node[model] (decoder) at (0, 5.8) {Decoder};
\node[model] (heads) at (2.5, 5.8) {Task Heads};

% Weight loading
\node[box, fill=orange!30] (loadweights) at (-1.2, 7.4) {\_load\_pretrained\_weights()};

% FLAN-T5
\node[box, fill=purple!20] (flant5) at (-4, 7.4) {FLAN-T5\\(HuggingFace)};

% Final model
\node[box, fill=red!20, minimum width=3cm] (mtmodel) at (0, 9) {MultiTaskModel};

% Arrows
\draw[arrow] (yaml) -- (loadconfig);
\draw[arrow] (loadconfig) -- (modelconfig);
\draw[arrow] (modelconfig) -- (build);
\draw[arrow] (build) -- (encoder);
\draw[arrow] (build) -- (decoder);
\draw[arrow] (build) -- (heads);
\draw[arrow] (encoder) -- (loadweights);
\draw[arrow] (decoder) -- (loadweights);
\draw[arrow] (flant5) -- (loadweights);
\draw[arrow] (loadweights) -- (mtmodel);
\draw[arrow] (heads) -- (mtmodel);

\end{tikzpicture}
\caption{Model construction pipeline in \texttt{factory.py}. Configuration is loaded from YAML, components are instantiated, FLAN-T5 weights are transferred, and the final MultiTaskModel is assembled.}
\label{fig:factory_flow}
\end{figure}

\subsection{Configuration Management}

The \texttt{ModelConfig} dataclass defines all architecture hyperparameters:

\begin{lstlisting}[caption={ModelConfig from factory.py}]
@dataclass
class ModelConfig:
    d_model: int = 768
    vocab_size: Optional[int] = None
    num_encoder_layers: int = 12
    num_decoder_layers: int = 12
    num_attention_heads: int = 12
    ffn_dim: int = 2048
    dropout: float = 0.1
    use_pretrained: bool = False
    pretrained_model_name: str = 
        "google/flan-t5-base"
    activation: str = "gated-gelu"
    use_relative_position_bias: bool = False
\end{lstlisting}

\subsection{Weight Transfer Mechanism}

The \texttt{\_load\_pretrained\_weights} function performs careful weight mapping between FLAN-T5 and LexiMind's custom architecture. Key considerations documented in the code:

\begin{quote}
\emph{``T5 architecture compatibility with our custom Transformer: T5 uses Pre-LN (RMSNorm before sublayers) --- matches our design; T5 uses relative position bias instead of absolute embeddings; T5 uses gated FFN (wi\_0, wi\_1, wo); T5 attention has no bias, our attention has bias --- we zero-initialize the bias terms.''} --- \texttt{factory.py}, lines 100--108
\end{quote}

Table \ref{tab:weight_mapping} shows the parameter correspondence:

\begin{table}[htbp]
\centering
\caption{FLAN-T5 to LexiMind Weight Mapping}
\label{tab:weight_mapping}
\begin{tabular}{ll}
\toprule
\textbf{FLAN-T5 Parameter} & \textbf{LexiMind Parameter} \\
\midrule
\texttt{shared} & \texttt{encoder.embedding} \\
\texttt{encoder.block.*.SelfAttention.q} & \texttt{encoder.layers.*.self\_attn.W\_Q} \\
\texttt{encoder.block.*.SelfAttention.k} & \texttt{encoder.layers.*.self\_attn.W\_K} \\
\texttt{encoder.block.*.SelfAttention.v} & \texttt{encoder.layers.*.self\_attn.W\_V} \\
\texttt{encoder.block.*.SelfAttention.o} & \texttt{encoder.layers.*.self\_attn.W\_O} \\
\texttt{*.layer\_norm} & \texttt{*.norm*} \\
\texttt{*.DenseReluDense.wi\_0} & \texttt{*.ffn.linear\_gate} \\
\texttt{*.DenseReluDense.wi\_1} & \texttt{*.ffn.linear1} \\
\texttt{*.DenseReluDense.wo} & \texttt{*.ffn.linear2} \\
\texttt{lm\_head} & \texttt{decoder.output\_projection} \\
\bottomrule
\end{tabular}
\end{table}

\subsection{Vocabulary Size Handling}

T5 pads its vocabulary to multiples of 128 for computational efficiency (32,100 → 32,128). LexiMind handles this mismatch:

\begin{quote}
\emph{``Note: T5's vocab is padded to multiple of 128 for efficiency (32100 → 32128). [...] Copy only the tokens that exist in both. Initialize any extra tokens with small random values.''} — \texttt{factory.py}, lines 116--131
\end{quote}

\subsection{Model Assembly}

The \texttt{build\_multitask\_model} function assembles the complete system:

\begin{lstlisting}[caption={Model assembly from factory.py}]
model = MultiTaskModel(
    encoder=encoder, 
    decoder=decoder,
    decoder_outputs_logits=True
)
model.add_head(
    "summarization",
    LMHead(d_model=cfg.d_model, 
           vocab_size=vocab_size,
           tie_embedding=decoder.embedding)
)
model.add_head(
    "emotion",
    ClassificationHead(
        d_model=cfg.d_model, 
        num_labels=28,  # GoEmotions
        pooler="mean",
        hidden_dim=cfg.d_model // 2)
)
model.add_head(
    "topic",
    ClassificationHead(
        d_model=cfg.d_model,
        num_labels=7,  # 7 topic categories
        pooler="mean")
)
\end{lstlisting}

%=============================================================================
\section{Multi-Task Model Architecture}
\label{sec:multitask}
%=============================================================================

The \texttt{MultiTaskModel} class in \texttt{src/models/multitask.py} provides the routing infrastructure for multi-task learning. Figure \ref{fig:multitask_routing} illustrates the task routing mechanism.

\begin{figure}[htbp]
\centering
\begin{tikzpicture}[
    scale=0.7,
    transform shape,
    box/.style={draw, rectangle, minimum width=2cm, minimum height=0.6cm, align=center, rounded corners=2pt},
    decision/.style={draw, diamond, aspect=2, minimum width=1.5cm, align=center, fill=yellow!30},
    arrow/.style={->, >=stealth, thick},
]

% Forward call
\node[box, fill=blue!20] (forward) at (0, 0) {forward(task, inputs)};

% Decision
\node[decision] (taskcheck) at (0, -1.5) {task type?};

% Branches
\node[box, fill=green!20] (encoder) at (-3.5, -3.5) {Encoder\\Only};
\node[box, fill=green!20] (seq2seq) at (3.5, -3.5) {Encoder\\+ Decoder};

% Heads
\node[box, fill=orange!20] (classhead) at (-3.5, -5) {Classification\\Head};
\node[box, fill=orange!20] (lmhead) at (3.5, -5) {LM Head};

% Tasks
\node[box, fill=purple!20, font=\scriptsize] (emotion) at (-5, -6.5) {Emotion};
\node[box, fill=purple!20, font=\scriptsize] (topic) at (-2, -6.5) {Topic};
\node[box, fill=purple!20, font=\scriptsize] (summ) at (3.5, -6.5) {Summarization};

% Arrows
\draw[arrow] (forward) -- (taskcheck);
\draw[arrow] (taskcheck) -- node[above, font=\scriptsize] {Classification} (encoder);
\draw[arrow] (taskcheck) -- node[above, font=\scriptsize] {Generation} (seq2seq);
\draw[arrow] (encoder) -- (classhead);
\draw[arrow] (seq2seq) -- (lmhead);
\draw[arrow] (classhead) -- (emotion);
\draw[arrow] (classhead) -- (topic);
\draw[arrow] (lmhead) -- (summ);

\end{tikzpicture}
\caption{Task routing in MultiTaskModel. Classification tasks use encoder-only processing with mean pooling, while generation tasks use the full encoder-decoder pipeline.}
\label{fig:multitask_routing}
\end{figure}

\subsection{Task-Specific Head Selection}

The forward method routes inputs based on head type:

\begin{quote}
\emph{``Encoder-only heads expect encoder outputs [...] LM/seq2seq head: run encoder → decoder → lm head''} — \texttt{multitask.py}, lines 108--148
\end{quote}

\subsection{Classification Head}

Classification tasks (emotion, topic) use mean pooling over encoder outputs:

\begin{equation}
h_{cls} = \frac{\sum_{i=1}^{L} h_i \cdot m_i}{\sum_{i=1}^{L} m_i}
\end{equation}

where $m_i$ is the attention mask (1 for valid tokens, 0 for padding). The pooled representation is projected through a linear layer to class logits.

%=============================================================================
\section{Training Pipeline}
\label{sec:training}
%=============================================================================

The training infrastructure in \texttt{src/training/trainer.py} implements a comprehensive multi-task training loop with modern deep learning practices.

\subsection{Training Configuration}

The \texttt{TrainerConfig} dataclass encapsulates all hyperparameters:

\begin{lstlisting}[caption={TrainerConfig from trainer.py}]
@dataclass
class TrainerConfig:
    max_epochs: int = 1
    gradient_clip_norm: float = 1.0
    task_weights: Dict[str, float] | None = None
    label_smoothing: float = 0.0
    gradient_accumulation_steps: int = 1
    scheduler_type: str = "cosine"
    warmup_steps: int = 0
    early_stopping_patience: int | None = None
    gradient_checkpointing: bool = False
    compile_model: bool = False
\end{lstlisting}

\subsection{Mixed-Precision Training}

LexiMind uses Automatic Mixed Precision (AMP) with automatic dtype selection:

\begin{quote}
\emph{``AMP setup: bfloat16 for Ampere+ GPUs, float16 otherwise''} — \texttt{trainer.py}, line 102
\end{quote}

BFloat16 provides better numerical stability for training while maintaining the memory and speed benefits of reduced precision.

\subsection{Learning Rate Scheduling}

A cosine schedule with linear warmup is implemented:

\begin{equation}
lr(t) = \begin{cases}
lr_{max} \cdot \frac{t}{t_{warmup}} & t < t_{warmup} \\
lr_{min} + \frac{1}{2}(lr_{max} - lr_{min})(1 + \cos(\frac{\pi(t-t_{warmup})}{T-t_{warmup}})) & t \geq t_{warmup}
\end{cases}
\end{equation}

\subsection{Multi-Task Loss Computation}

The total loss combines task-specific losses with optional weighting:

\begin{equation}
\mathcal{L}_{total} = \sum_{t \in \text{tasks}} \lambda_t \mathcal{L}_t
\end{equation}

\begin{itemize}
    \item \textbf{Summarization}: Cross-entropy with label smoothing and \texttt{ignore\_index=-100}
    \item \textbf{Emotion}: Binary Cross-Entropy with Logits (multi-label)
    \item \textbf{Topic}: Standard Cross-Entropy (single-label)
\end{itemize}

\subsection{Gradient Handling}

The trainer includes gradient clipping and early stopping:

\begin{quote}
\emph{``Gradient clipping to prevent exploding gradients [...] Early stopping based on validation loss''} — \texttt{trainer.py}
\end{quote}

\subsection{Training Loop}

Figure \ref{fig:training_loop} illustrates the training loop structure.

\begin{figure}[htbp]
\centering
\begin{tikzpicture}[
    scale=0.65,
    transform shape,
    box/.style={draw, rectangle, minimum width=2.5cm, minimum height=0.5cm, align=center, rounded corners=2pt, font=\small},
    arrow/.style={->, >=stealth},
]

% Epoch loop
\node[box, fill=blue!20] (epoch) at (0, 0) {For each epoch};

% Batch loop
\node[box, fill=green!20] (batch) at (0, -1.2) {For each batch};

% Task loop
\node[box, fill=yellow!20] (task) at (0, -2.4) {For each task};

% Forward
\node[box, fill=orange!20] (forward) at (0, -3.6) {Forward + Loss};

% AMP context
\node[box, fill=purple!20] (amp) at (0, -4.8) {AMP autocast};

% Backward
\node[box, fill=red!20] (backward) at (0, -6) {Backward (scaled)};

% Accumulate check
\node[box, fill=cyan!20] (accum) at (0, -7.2) {Accumulation step?};

% Optimizer step
\node[box, fill=gray!20] (optim) at (0, -8.4) {Clip + Step + Zero};

% Validation
\node[box, fill=blue!20] (val) at (3.5, -1.2) {Validation};

% Checkpoint
\node[box, fill=green!20] (ckpt) at (3.5, -2.4) {Checkpoint};

% Early stopping
\node[box, fill=red!20] (early) at (3.5, -3.6) {Early Stop?};

% Arrows
\draw[arrow] (epoch) -- (batch);
\draw[arrow] (batch) -- (task);
\draw[arrow] (task) -- (forward);
\draw[arrow] (forward) -- (amp);
\draw[arrow] (amp) -- (backward);
\draw[arrow] (backward) -- (accum);
\draw[arrow] (accum) -- (optim);
\draw[arrow] (optim.south) -- ++(0, -0.3) -| ++(-2, 0) |- (task.west);
\draw[arrow] (epoch.east) -- ++(0.5, 0) |- (val);
\draw[arrow] (val) -- (ckpt);
\draw[arrow] (ckpt) -- (early);

\end{tikzpicture}
\caption{Training loop structure showing nested iteration over epochs, batches, and tasks, with gradient accumulation and validation checkpoints.}
\label{fig:training_loop}
\end{figure}

%=============================================================================
\section{Tasks and Datasets}
%=============================================================================

LexiMind addresses three complementary NLP tasks:

\subsection{Text Summarization}

\textbf{Task}: Generate concise abstractive summaries from longer documents, focusing on back-cover style book descriptions.

\textbf{Datasets}: A combination of Goodreads book descriptions ($\sim$49K samples) matched with Project Gutenberg full texts for literary summarization, and arXiv academic paper abstracts for technical domain coverage. Unlike news-focused models, LexiMind specializes in literary and academic long-form content understanding.

\textbf{Approach}: Encoder-decoder generation with beam search decoding. The decoder uses causal masking and cross-attention to encoder representations.

\textbf{Evaluation}: ROUGE-1/2/L, BLEU-4, and BERTScore (using RoBERTa-large) measuring both n-gram overlap and semantic similarity between generated and reference summaries.

\subsection{Emotion Classification}

\textbf{Task}: Multi-label classification identifying emotions in text.

\textbf{Dataset}: Google's GoEmotions (43K Reddit comments)

\textbf{Classes}: 28 emotions including admiration, amusement, anger, annoyance, approval, caring, confusion, curiosity, desire, disappointment, disapproval, disgust, embarrassment, excitement, fear, gratitude, grief, joy, love, nervousness, optimism, pride, realization, relief, remorse, sadness, surprise, and neutral.

\textbf{Approach}: Encoder-only with mean pooling, followed by a linear projection. Binary Cross-Entropy loss enables multi-label prediction.

\subsection{Topic Classification}

\textbf{Task}: Single-label classification of document topics.

\textbf{Datasets}: arXiv papers and Project Gutenberg books ($\sim$3.4K samples), providing topic classification across academic and literary domains.

\textbf{Classes}: 7 topics (Arts, Business, Fiction, History, Philosophy, Science, Technology)

\textbf{Approach}: Same encoder-only architecture as emotion classification, but with standard Cross-Entropy loss for mutually exclusive classes. Due to the smaller dataset size, topic weight is reduced during training to prevent overfitting.

%=============================================================================
\section{Model Specifications}
%=============================================================================

Table \ref{tab:model_specs} summarizes LexiMind's architecture, aligned with FLAN-T5-base for weight compatibility.

\begin{table}[htbp]
\centering
\caption{LexiMind Model Specifications}
\label{tab:model_specs}
\begin{tabular}{lc}
\toprule
\textbf{Parameter} & \textbf{Value} \\
\midrule
Hidden dimension ($d_{model}$) & 768 \\
FFN dimension ($d_{ff}$) & 2048 \\
Attention heads & 12 \\
Head dimension & 64 \\
Encoder layers & 12 \\
Decoder layers & 12 \\
Vocabulary size & 32,128 \\
Max sequence length & 512 \\
Dropout & 0.1 \\
Activation & Gated-GELU \\
Normalization & RMSNorm (Pre-LN) \\
Position encoding & Relative bias \\
\midrule
Total parameters & $\sim$272M \\
\bottomrule
\end{tabular}
\end{table}

%=============================================================================
\section{Implementation Details}
%=============================================================================

\subsection{Project Structure}

LexiMind follows a modular architecture:

\begin{verbatim}
src/
+-- models/
|   +-- attention.py     # MHA, RelPosBias
|   +-- encoder.py       # Encoder blocks
|   +-- decoder.py       # Decoder blocks  
|   +-- heads.py         # Task heads
|   +-- multitask.py     # MTL routing
|   +-- factory.py       # Construction
+-- data/
|   +-- tokenization.py  # Tokenizer
|   +-- dataset.py       # Dataset classes
|   +-- dataloader.py    # Collators
+-- training/
    +-- trainer.py       # Training loop
    +-- metrics.py       # Evaluation
scripts/
+-- train.py             # Main training
+-- download_data.py     # Dataset download
+-- inference.py         # CLI inference
+-- demo_gradio.py       # Web demo
\end{verbatim}

\subsection{FlashAttention and CUDA Optimizations}

The trainer enables comprehensive hardware-specific optimizations:

\begin{lstlisting}[caption={CUDA optimizations from train.py}]
if device.type == "cuda":
    torch.backends.cudnn.benchmark = True
    torch.backends.cuda.matmul.allow_tf32 = True
    torch.backends.cudnn.allow_tf32 = True
    torch.backends.cuda.enable_flash_sdp(True)
    torch.backends.cuda.enable_mem_efficient_sdp(
        True)
\end{lstlisting}

Note that T5-style relative position bias is incompatible with FlashAttention, as FlashAttention requires adding bias tensors to attention scores which breaks the fused kernel. The development configuration disables relative position bias to enable FlashAttention for faster iteration, while production configurations retain relative position bias for better quality.

\subsection{Numerical Stability}

To prevent overflow during mixed-precision training, hidden states are clamped after each sublayer:

\begin{quote}
\emph{``Clamp inf values for fp16/bf16 training stability (like HuggingFace T5)''} — \texttt{encoder.py}, lines 103--105
\end{quote}

%=============================================================================
\section{Experimental Setup}
%=============================================================================

\subsection{Training Configuration}

The final training configuration was optimized for quality and efficiency on an NVIDIA RTX 4070 with 12GB VRAM:

\begin{itemize}
    \item \textbf{Optimizer}: Fused AdamW with weight decay 0.01, $\beta_1=0.9$, $\beta_2=0.98$
    \item \textbf{Learning Rate}: $3 \times 10^{-5}$ with cosine decay
    \item \textbf{Warmup}: 300 steps ($\sim$0.5 epochs)
    \item \textbf{Batch Size}: 10 with 4$\times$ gradient accumulation (effective batch size 40)
    \item \textbf{Precision}: BFloat16 on Ampere+ GPUs with TF32 enabled
    \item \textbf{Gradient Clipping}: Max norm 1.0
    \item \textbf{Gradient Checkpointing}: Enabled for memory efficiency
    \item \textbf{torch.compile}: Dynamic compilation for encoder and decoder
    \item \textbf{Task Weights}: Summarization 1.0, Emotion 1.0, Topic 0.3 (reduced due to small dataset)
    \item \textbf{Early Stopping}: Patience of 3 epochs on validation loss
    \item \textbf{Encoder Freezing}: Bottom 4 layers frozen for stable transfer learning
\end{itemize}

Training completed in 7 epochs ($\sim$6 hours) with early stopping triggered due to validation loss plateau.

%=============================================================================
\section{Experimental Results}
\label{sec:results}
%=============================================================================

We evaluate LexiMind on held-out validation sets for each task. Table \ref{tab:summarization_results} presents the summarization metrics, Table \ref{tab:classification_results} shows classification performance.

\subsection{Summarization Performance}

\begin{table}[htbp]
\centering
\caption{Summarization Evaluation Results}
\label{tab:summarization_results}
\begin{tabular}{lc}
\toprule
\textbf{Metric} & \textbf{Score} \\
\midrule
ROUGE-1 & 0.3064 \\
ROUGE-2 & 0.0896 \\
ROUGE-L & 0.1832 \\
BLEU-4 & 0.0237 \\
\midrule
BERTScore Precision & 0.8430 \\
BERTScore Recall & 0.8179 \\
\textbf{BERTScore F1} & \textbf{0.8300} \\
\bottomrule
\end{tabular}
\end{table}

The BERTScore F1 of \textbf{0.83} demonstrates strong semantic similarity between generated descriptions and references, indicating the model captures meaning effectively even when exact wording differs. ROUGE scores are typical for abstractive summarization where the model paraphrases rather than extracts verbatim text.

\subsection{Classification Performance}

\begin{table}[htbp]
\centering
\caption{Classification Evaluation Results}
\label{tab:classification_results}
\begin{tabular}{llc}
\toprule
\textbf{Task} & \textbf{Metric} & \textbf{Score} \\
\midrule
\multirow{2}{*}{Topic (7 classes)} & Accuracy & \textbf{85.19\%} \\
 & Macro F1 & 0.8474 \\
\midrule
Emotion (28 classes) & Multi-label F1 & 0.1987 \\
\bottomrule
\end{tabular}
\end{table}

Topic classification achieves \textbf{85.2\%} accuracy with balanced per-class performance. The emotion detection task proves more challenging due to the 28-class multi-label setting with inherent label ambiguity in the GoEmotions dataset.

\subsection{Training Dynamics}

Figure \ref{fig:training_curves} shows the training dynamics over 7 epochs. The model converges smoothly with cosine learning rate decay, achieving best validation performance at epoch 4-5 before early stopping.

\begin{figure}[htbp]
\centering
\includegraphics[width=\columnwidth]{figures/training_loss_curve.png}
\caption{Training loss curves showing convergence over 7 epochs. Early stopping triggered after epoch 7 due to validation loss plateau.}
\label{fig:training_curves}
\end{figure}

Figure \ref{fig:task_metrics} presents per-task metrics throughout training, showing the distinct learning trajectories for summarization, emotion detection, and topic classification.

\begin{figure}[htbp]
\centering
\includegraphics[width=\columnwidth]{figures/task_metrics.png}
\caption{Task-specific metrics during training: ROUGE-1 for summarization, F1 for emotion detection, and accuracy for topic classification.}
\label{fig:task_metrics}
\end{figure}

\subsection{Per-Class Topic Analysis}

Table \ref{tab:topic_breakdown} shows the per-class performance for topic classification:

\begin{table}[htbp]
\centering
\caption{Per-Class Topic Classification Performance}
\label{tab:topic_breakdown}
\begin{tabular}{lccc}
\toprule
\textbf{Topic} & \textbf{Precision} & \textbf{Recall} & \textbf{F1} \\
\midrule
Arts & 0.93 & 0.76 & 0.84 \\
Business & 0.97 & 0.97 & 0.97 \\
Fiction & 0.95 & 1.00 & 0.97 \\
History & 0.83 & 0.78 & 0.81 \\
Philosophy & 0.80 & 0.86 & 0.83 \\
Science & 0.58 & 0.73 & 0.65 \\
Technology & 0.86 & 0.89 & 0.87 \\
\bottomrule
\end{tabular}
\end{table}

The model performs best on Fiction and Business categories, while Science shows the most confusion, likely due to overlap with Technology topics.

%=============================================================================
\section{Discussion}
%=============================================================================

\subsection{Key Findings}

\textbf{BERTScore vs. ROUGE}: The high BERTScore (0.83) combined with moderate ROUGE scores (0.31 ROUGE-1) illustrates a key characteristic of abstractive summarization. The model generates semantically accurate paraphrases rather than extractive copies, which ROUGE under-penalizes. BERTScore's contextual embeddings better capture this semantic fidelity.

\textbf{Multi-Task Trade-offs}: The reduced topic weight (0.3) was necessary to prevent overfitting on the small 3.4K sample dataset. Despite cycling through the topic data 14 times per epoch, the model achieves strong generalization with 85\% test accuracy.

\textbf{Transfer Learning Benefits}: Initializing from FLAN-T5-base provides strong linguistic priors, enabling competitive performance with only 7 epochs of fine-tuning. Freezing the bottom 4 encoder layers stabilizes training while allowing upper layers to adapt to our specific tasks.

\subsection{Limitations}

\begin{itemize}
    \item \textbf{Emotion Detection}: The 28-class multi-label setting remains challenging. GoEmotions' Reddit-sourced data may not generalize well to literary content.
    \item \textbf{Topic Dataset Size}: Only 3.4K topic samples limits the model's exposure to diverse examples.
    \item \textbf{Computational Resources}: Training requires $\sim$10GB VRAM, limiting accessibility on lower-end hardware.
\end{itemize}

\subsection{Experiment Tracking}

All experiments are tracked with MLflow:

\begin{quote}
\emph{``Metrics in src/training/metrics.py include accuracy, multi-label F1, and ROUGE-like overlap''} — architecture documentation
\end{quote}

%=============================================================================
\section{Conclusion}
%=============================================================================

LexiMind demonstrates that building Transformer architectures from scratch while leveraging pre-trained weights provides a powerful combination of transparency, flexibility, and performance. The hybrid approach---custom implementation with FLAN-T5 weight initialization---enables:

\begin{enumerate}
    \item Full understanding and control over architectural decisions
    \item Seamless adaptation to multi-task learning scenarios
    \item Transfer of linguistic knowledge from large-scale pre-training
    \item Integration of modern optimizations (FlashAttention, RMSNorm)
\end{enumerate}

Our experimental results validate this approach:
\begin{itemize}
    \item \textbf{Summarization}: BERTScore F1 of 0.83 demonstrates strong semantic fidelity
    \item \textbf{Topic Classification}: 85.2\% accuracy across 7 categories
    \item \textbf{Emotion Detection}: Competitive multi-label performance on 28 classes
\end{itemize}

The modular design of LexiMind's codebase facilitates extension to new tasks, experimentation with architectural variants, and serves as an educational resource for understanding Transformer internals. The complete system trains efficiently on consumer GPUs ($\sim$6 hours on RTX 4070 12GB).

Future work may explore integration of Parameter-Efficient Fine-Tuning (PEFT) methods such as Low-Rank Adaptation (LoRA) \cite{hu2022lora}, expansion of the topic classification dataset, and scaling to larger architectures such as FLAN-T5-large or FLAN-T5-xl.

%=============================================================================
% References
%=============================================================================

\begin{thebibliography}{00}

\bibitem{vaswani2017attention}
A. Vaswani, N. Shazeer, N. Parmar, J. Uszkoreit, L. Jones, A. N. Gomez, Ł. Kaiser, and I. Polosukhin, ``Attention is all you need,'' in \textit{Advances in Neural Information Processing Systems (NeurIPS)}, vol. 30, 2017, pp. 5998--6008. [Online]. Available: \url{https://arxiv.org/abs/1706.03762}

\bibitem{raffel2020exploring}
C. Raffel, N. Shazeer, A. Roberts, K. Lee, S. Narang, M. Matena, Y. Zhou, W. Li, and P. J. Liu, ``Exploring the limits of transfer learning with a unified text-to-text transformer,'' \textit{Journal of Machine Learning Research}, vol. 21, no. 140, pp. 1--67, 2020. [Online]. Available: \url{https://arxiv.org/abs/1910.10683}

\bibitem{chung2022scaling}
H. W. Chung, L. Hou, S. Longpre, B. Zoph, Y. Tay, W. Fedus, Y. Li, X. Wang, M. Dehghani, S. Brahma, A. Webson, S. S. Gu, Z. Dai, M. Suzgun, X. Chen, A. Chowdhery, A. Castro-Ros, M. Pellat, K. Robinson, D. Valter, S. Narang, G. Mishra, A. Yu, V. Zhao, Y. Huang, A. Dai, H. Yu, S. Petrov, E. H. Chi, J. Dean, J. Devlin, A. Roberts, D. Zhou, Q. V. Le, and J. Wei, ``Scaling instruction-finetuned language models,'' \textit{arXiv preprint arXiv:2210.11416}, 2022. [Online]. Available: \url{https://arxiv.org/abs/2210.11416}

\bibitem{xiong2020layer}
R. Xiong, Y. Yang, J. He, K. Zheng, S. Zheng, C. Xing, H. Zhang, Y. Lan, L. Wang, and T. Liu, ``On layer normalization in the transformer architecture,'' in \textit{International Conference on Machine Learning (ICML)}, 2020, pp. 10524--10533. [Online]. Available: \url{https://arxiv.org/abs/2002.04745}

\bibitem{zhang2019root}
B. Zhang and R. Sennrich, ``Root mean square layer normalization,'' in \textit{Advances in Neural Information Processing Systems (NeurIPS)}, vol. 32, 2019, pp. 12360--12371. [Online]. Available: \url{https://arxiv.org/abs/1910.07467}

\bibitem{ba2016layer}
J. L. Ba, J. R. Kiros, and G. E. Hinton, ``Layer normalization,'' \textit{arXiv preprint arXiv:1607.06450}, 2016. [Online]. Available: \url{https://arxiv.org/abs/1607.06450}

\bibitem{caruana1997multitask}
R. Caruana, ``Multitask learning,'' \textit{Machine Learning}, vol. 28, no. 1, pp. 41--75, 1997.

\bibitem{kudo2018sentencepiece}
T. Kudo and J. Richardson, ``SentencePiece: A simple and language independent subword tokenizer and detokenizer for neural text processing,'' in \textit{Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing: System Demonstrations}, 2018, pp. 66--71. [Online]. Available: \url{https://arxiv.org/abs/1808.06226}

\bibitem{hu2022lora}
E. J. Hu, Y. Shen, P. Wallis, Z. Allen-Zhu, Y. Li, S. Wang, L. Wang, and W. Chen, ``LoRA: Low-rank adaptation of large language models,'' in \textit{International Conference on Learning Representations (ICLR)}, 2022. [Online]. Available: \url{https://arxiv.org/abs/2106.09685}

\bibitem{dao2022flashattention}
T. Dao, D. Fu, S. Ermon, A. Rudra, and C. Ré, ``FlashAttention: Fast and memory-efficient exact attention with IO-awareness,'' in \textit{Advances in Neural Information Processing Systems (NeurIPS)}, vol. 35, 2022, pp. 16344--16359. [Online]. Available: \url{https://arxiv.org/abs/2205.14135}

\bibitem{zhang2019bertscore}
T. Zhang, V. Kishore, F. Wu, K. Q. Weinberger, and Y. Artzi, ``BERTScore: Evaluating text generation with BERT,'' in \textit{International Conference on Learning Representations (ICLR)}, 2020. [Online]. Available: \url{https://arxiv.org/abs/1904.09675}

\bibitem{demszky2020goemotions}
D. Demszky, D. Movshovitz-Attias, J. Ko, A. Cowen, G. Nemade, and S. Ravi, ``GoEmotions: A dataset of fine-grained emotions,'' in \textit{Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics}, 2020, pp. 4040--4054. [Online]. Available: \url{https://arxiv.org/abs/2005.00547}

\end{thebibliography}

\end{document}