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
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
// Copyright 2018 Google AI and Google Brain team.

// Copyright 2018 Carnegie Mellon University Authors.

// Copyright 2020-present, the HuggingFace Inc. team.

// Copyright 2020 Guillaume Becquin

// Licensed under the Apache License, Version 2.0 (the "License");

// you may not use this file except in compliance with the License.

// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software

// distributed under the License is distributed on an "AS IS" BASIS,

// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

// See the License for the specific language governing permissions and

// limitations under the License.


use crate::common::activations::Activation;
use crate::common::dropout::Dropout;
use crate::common::summary::{SequenceSummary, SummaryConfig, SummaryType};
use crate::pipelines::generation_utils::{Cache, LMHeadModel, LMModelOutput};
use crate::xlnet::attention::LayerState;
use crate::xlnet::encoder::XLNetLayer;
use crate::{Config, RustBertError};
use serde::{Deserialize, Serialize};
use std::borrow::{Borrow, BorrowMut};
use std::collections::HashMap;
use tch::nn::Init;
use tch::{nn, Device, Kind, Tensor};

/// # XLNet Pretrained model weight files

pub struct XLNetModelResources;

/// # XLNet Pretrained model config files

pub struct XLNetConfigResources;

/// # XLNet Pretrained model vocab files

pub struct XLNetVocabResources;

impl XLNetModelResources {
    /// Shared under Apache 2.0 license by the XLNet Authors at https://github.com/zihangdai/xlnet. Modified with conversion to C-array format.

    pub const XLNET_BASE_CASED: (&'static str, &'static str) = (
        "xlnet-base-cased/model",
        "https://cdn.huggingface.co/xlnet-base-cased-rust_model.ot",
    );
}

impl XLNetConfigResources {
    /// Shared under Apache 2.0 license by the XLNet Authors at https://github.com/zihangdai/xlnet. Modified with conversion to C-array format.

    pub const XLNET_BASE_CASED: (&'static str, &'static str) = (
        "xlnet-base-cased/config",
        "https://cdn.huggingface.co/xlnet-base-cased-config.json",
    );
}

impl XLNetVocabResources {
    /// Shared under Apache 2.0 license by the XLNet Authors at https://github.com/zihangdai/xlnet. Modified with conversion to C-array format.

    pub const XLNET_BASE_CASED: (&'static str, &'static str) = (
        "xlnet-base-cased/spiece",
        "https://cdn.huggingface.co/xlnet-base-cased-spiece.model",
    );
}

#[allow(non_camel_case_types)]
#[derive(Clone, Debug, Serialize, Deserialize, Copy)]
/// # Attention type for the model (bidirectional or unidirectional)

pub enum AttentionType {
    /// Bidirectional (XLNet)

    bi,
    /// Unidirectional (Transformer-XL)

    uni,
}

#[derive(Debug, Serialize, Deserialize)]
/// # XLNet model configuration

/// Defines the XLNet model architecture (e.g. number of layers, hidden layer size, label mapping...)

pub struct XLNetConfig {
    pub vocab_size: i64,
    pub d_model: i64,
    pub n_layer: i64,
    pub d_head: i64,
    pub n_head: i64,
    pub d_inner: i64,
    pub ff_activation: Activation,
    pub untie_r: bool,
    pub attn_type: AttentionType,
    pub initializer_range: f32,
    pub layer_norm_eps: Option<f64>,
    pub dropout: f64,
    pub mem_len: Option<i64>,
    pub reuse_len: Option<i64>,
    pub clamp_len: Option<i64>,
    pub bi_data: bool,
    pub same_length: bool,
    pub summary_type: Option<SummaryType>,
    pub summary_use_proj: Option<bool>,
    pub summary_activation: Option<Activation>,
    pub summary_proj_to_labels: Option<bool>,
    pub summary_first_dropout: Option<f64>,
    pub summary_last_dropout: Option<f64>,
    pub start_n_top: Option<i64>,
    pub end_n_top: Option<i64>,
    pub use_cache: Option<bool>,
    pub bos_token_id: i64,
    pub eos_token_id: i64,
    pub pad_token_id: i64,
    pub id2label: Option<HashMap<i64, String>>,
    pub label2id: Option<HashMap<String, i64>>,
    pub output_attentions: Option<bool>,
    pub output_hidden_states: Option<bool>,
    pub chunk_size_feed_forward: Option<i64>,
}

impl Config<XLNetConfig> for XLNetConfig {}

/// # XLNet Base model

/// Base architecture for XLNet models. Task-specific models will be built from this common base model

/// It is made of the following blocks:

/// - `word_embeddings`: Word embeddings

/// - `mask_emb`: Embedding for the query stream

/// - `layers`: Vector of `XLNetLayer`. Each layer is made of a self-attention layers on the visible and hidden states and a post-attention layer

pub struct XLNetModel {
    mem_len: Option<i64>,
    reuse_len: Option<i64>,
    same_length: bool,
    attention_type: AttentionType,
    bi_data: bool,
    clamp_len: Option<i64>,
    d_model: i64,
    word_embeddings: nn::Embedding,
    mask_emb: Tensor,
    layers: Vec<XLNetLayer>,
    dropout: Dropout,
    output_attentions: bool,
    output_hidden_states: bool,
    use_cache: bool,
}

impl XLNetModel {
    /// Build a new `XLNetModel`

    ///

    /// # Arguments

    ///

    /// * `p` - Variable store path for the root of the XLNet model

    /// * `config` - `XLNetConfig` object defining the model architecture

    ///

    /// # Example

    ///

    /// ```no_run

    /// use rust_bert::xlnet::{XLNetConfig, XLNetModel};

    /// use rust_bert::Config;

    /// use std::path::Path;

    /// use tch::{nn, Device};

    ///

    /// let config_path = Path::new("path/to/config.json");

    /// let device = Device::Cpu;

    /// let p = nn::VarStore::new(device);

    /// let config = XLNetConfig::from_file(config_path);

    /// let xlnet_model = XLNetModel::new(&p.root(), &config);

    /// ```

    pub fn new<'p, P>(p: P, config: &XLNetConfig) -> XLNetModel
    where
        P: Borrow<nn::Path<'p>>,
    {
        let p = p.borrow();

        let mem_len = config.mem_len;
        let reuse_len = config.reuse_len;
        let same_length = config.same_length;
        let attention_type = config.attn_type;
        let bi_data = config.bi_data;
        let clamp_len = config.clamp_len;
        let d_model = config.d_model;

        let word_embeddings: nn::Embedding = nn::embedding(
            p / "word_embedding",
            config.vocab_size,
            config.d_model,
            Default::default(),
        );

        let mask_emb = p.var("mask_emb", &[1, 1, config.d_model], Init::Const(0f64));
        let mut layers: Vec<XLNetLayer> = vec![];
        let p_layers = p / "layer";
        for layer_index in 0..config.n_layer {
            layers.push(XLNetLayer::new(&p_layers / layer_index, config));
        }

        let dropout = Dropout::new(config.dropout);
        let use_cache = config.use_cache.unwrap_or(true);
        let output_attentions = config.output_attentions.unwrap_or(false);
        let output_hidden_states = config.output_hidden_states.unwrap_or(false);
        XLNetModel {
            mem_len,
            reuse_len,
            same_length,
            attention_type,
            bi_data,
            clamp_len,
            d_model,
            word_embeddings,
            mask_emb,
            layers,
            dropout,
            output_attentions,
            output_hidden_states,
            use_cache,
        }
    }

    fn create_mask(&self, q_len: i64, m_len: i64, device: Device) -> Tensor {
        let attention_mask = Tensor::ones(&[q_len, q_len], (Kind::Int64, device));
        let attention_mask_pad = Tensor::zeros(&[q_len, m_len], (Kind::Int64, device));
        let mask_up = attention_mask.triu(1);
        let mut output = Tensor::cat(&[&attention_mask_pad, &mask_up], 1);
        if self.same_length {
            let mask_low = attention_mask.tril(-1);
            output = Tensor::cat(
                &[
                    output.slice(1, 0, q_len, 1) + mask_low,
                    output.slice(1, q_len, q_len + m_len, 1),
                ],
                1,
            );
        }
        output
    }

    fn cache_mem(
        &self,
        current_output: &Tensor,
        previous_cached_state: &Option<LayerState>,
    ) -> LayerState {
        let cutoff = match self.mem_len {
            None => 0i64,
            Some(0) => 0i64,
            Some(value) => -value,
        };
        let mut cur_length = current_output.size()[0];
        LayerState {
            prev_content: match (self.reuse_len, previous_cached_state) {
                (Some(value), Some(previous_past)) if value > 0 => {
                    let current_output = current_output.slice(0, 0, value, 1);
                    cur_length += &previous_past.prev_content.size()[0];
                    Tensor::cat(&[&previous_past.prev_content, &current_output], 0)
                        .slice(0, cutoff, cur_length, 1)
                }
                (Some(_), Some(previous_past)) | (None, Some(previous_past)) => {
                    cur_length += &previous_past.prev_content.size()[0];
                    Tensor::cat(&[&previous_past.prev_content, current_output], 0)
                        .slice(0, cutoff, cur_length, 1)
                }
                (Some(value), None) if value > 0 => {
                    let current_output = current_output.slice(0, 0, value, 1);
                    current_output.slice(0, cutoff, cur_length, 1)
                }
                (Some(_), None) | (None, None) => current_output.slice(0, cutoff, cur_length, 1),
            },
        }
    }

    fn positional_embedding(
        &self,
        position_sequence: &Tensor,
        inverse_frequency: &Tensor,
        batch_size: Option<i64>,
    ) -> Tensor {
        let sinusoid = Tensor::einsum("i,d->id", &[position_sequence, inverse_frequency]);
        let mut positional_embeddings =
            Tensor::cat(&[sinusoid.sin(), sinusoid.cos()], -1).unsqueeze(1);

        if let Some(bsz) = batch_size {
            positional_embeddings = positional_embeddings.expand(&[-1, bsz, -1], true)
        };
        positional_embeddings
    }

    fn relative_positional_encoding(
        &self,
        q_len: i64,
        k_len: i64,
        batch_size: Option<i64>,
        device: Device,
    ) -> Tensor {
        let frequency_sequence = Tensor::arange2(0, self.d_model, 2, (Kind::Float, device));
        let inverse_frequency = 1f64 / Tensor::pow2(10000f64, &(frequency_sequence / self.d_model));
        let (begin, end) = match self.attention_type {
            AttentionType::bi => (k_len, -q_len),
            AttentionType::uni => (k_len, -1),
        };
        let mut forward_positions_sequence = Tensor::arange2(begin, end, -1, (Kind::Float, device));
        match self.clamp_len {
            Some(clamp_value) if clamp_value > 0 => {
                let _ = forward_positions_sequence.clamp_(-clamp_value, clamp_value);
            }
            _ => {}
        }
        if self.bi_data {
            let mut backward_positions_sequence =
                Tensor::arange2(-begin, -end, 1, (Kind::Float, device));
            match self.clamp_len {
                Some(clamp_value) if clamp_value > 0 => {
                    let _ = backward_positions_sequence.clamp_(-clamp_value, clamp_value);
                }
                _ => {}
            }
            let bsz = match batch_size {
                Some(value) => Some(value / 2),
                None => None,
            };

            let forward_positions_embeddings =
                self.positional_embedding(&forward_positions_sequence, &inverse_frequency, bsz);
            let backward_positions_embeddings =
                self.positional_embedding(&backward_positions_sequence, &inverse_frequency, bsz);
            Tensor::cat(
                &[forward_positions_embeddings, backward_positions_embeddings],
                1,
            )
        } else {
            self.positional_embedding(&forward_positions_sequence, &inverse_frequency, batch_size)
        }
    }

    /// Forward pass through the model

    ///

    /// # Arguments

    ///

    /// * `input_ids` - Optional input tensor of shape (*batch size*, *sequence_length*). This or `input_embeds` must be provided.

    /// * `attention_mask` - Optional attention mask of shape (*batch size*, *sequence_length*) for the encoder positions. Positions with a mask with value 0 will be masked.

    /// * `perm_mask` - Optional tensor of shape (*batch size*, *sequence_length*, *sequence_length*). Mask to indicate the attention pattern for each input token (only used for pre-training over permutations, rather than simple token masking).

    /// * `target_mapping ` - Optional tensor of shape (*batch size*, *num_tokens*, *sequence_length*) indicating the position of the masked words to predict.

    /// * `token_type_ids` - Optional tensor (*batch size*, *sequence_length*) indicating the sentence ID of the token (0: first sentence, 1: second sentence).

    /// * `input_embeds` - Optional input tensor of shape (*batch size*, *sequence_length*, *embeddings dimension*). This or `input_ids` must be provided.

    /// * `old_layer_states` - Optional vector of length `n_layer` containing optional `LayerStates` containing the last calculated content for the attention layers. This avoids recomputing attention weights at past positions and speeds up decoding.

    /// * `train` - boolean flag to turn on/off the dropout layers in the model. Should be set to false for inference.

    ///

    /// # Returns

    ///

    /// * `XLNetModelOutput` containing:

    ///   - `hidden_state` - `Tensor` of shape (*batch size*, *sequence_length*, *hidden_size*) representing the activations of the last hidden state

    ///   - `next_cache` - `Option<Vec<Option<LayerState>>>` of length *n_layer* containing the past content for the the attention layers with shape (*past_sequence_length*, *batch size*, *hidden_size*)

    ///   - `all_hidden_states` - `Option<Vec<(Tensor, Option<Tensor>)>>` of length *n_layer* with shape (*batch size*, *sequence_length*, *hidden_size*) (with optional query stream states if used)

    ///   - `all_attentions` - `Option<Vec<(Tensor, Option<Tensor>)>>` of length *n_layer* with shape (*batch size*, *sequence_length*, *hidden_size*) (with optional query stream states if used)

    ///

    /// # Example

    ///

    /// ```no_run

    /// # use tch::{nn, Device, Tensor, no_grad, Kind};

    /// # use rust_bert::Config;

    /// # use std::path::Path;

    /// # use tch::kind::Kind::{Int64, Double};

    /// use rust_bert::xlnet::{XLNetConfig, XLNetModel};

    /// # let config_path = Path::new("path/to/config.json");

    /// # let vocab_path = Path::new("path/to/vocab.txt");

    /// # let device = Device::Cpu;

    /// # let vs = nn::VarStore::new(device);

    /// # let config = XLNetConfig::from_file(config_path);

    /// # let xlnet_model: XLNetModel = XLNetModel::new(&vs.root(), &config);

    /// let (batch_size, sequence_length) = (64, 128);

    /// let input_tensor = Tensor::rand(&[batch_size, sequence_length], (Int64, device));

    /// let attention_mask = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_tensor = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_mapping = Tensor::zeros(&[64, 1, 128], (Kind::Float, device));

    /// let _ = target_mapping.narrow(2, 3, 1).fill_(1.0);

    ///

    /// let model_output = no_grad(|| {

    ///     xlnet_model.forward_t(

    ///         Some(&input_tensor),

    ///         Some(&attention_mask),

    ///         None,

    ///         Some(&target_mapping),

    ///         None,

    ///         None,

    ///         None,

    ///         false,

    ///     )

    /// });

    /// ```

    pub fn forward_t(
        &self,
        input_ids: Option<&Tensor>,
        attention_mask: Option<&Tensor>,
        old_layer_states: Option<Vec<Option<LayerState>>>,
        perm_mask: Option<&Tensor>,
        target_mapping: Option<&Tensor>,
        token_type_ids: Option<&Tensor>,
        input_embeds: Option<Tensor>,
        train: bool,
    ) -> Result<XLNetModelOutput, RustBertError> {
        let (word_emb_k, input_shape) = match input_ids {
            Some(input_value) => match input_embeds {
                Some(_) => {
                    return Err(RustBertError::ValueError(
                        "Only one of input ids or input embeddings may be set".into(),
                    ));
                }
                None => {
                    let size = input_value.size();
                    (
                        input_value
                            .transpose(0, 1)
                            .contiguous()
                            .apply_t(&self.word_embeddings, train),
                        vec![size[1], size[0]],
                    )
                }
            },
            None => match input_embeds {
                Some(embeds) => {
                    let size = vec![embeds.size()[1], embeds.size()[0]];
                    (embeds.transpose(0, 1).contiguous(), size)
                }
                None => {
                    return Err(RustBertError::ValueError(
                        "At least one of input ids or input embeddings must be set".into(),
                    ));
                }
            },
        };

        let token_type_ids = match token_type_ids {
            Some(token_type_ids) => Some(token_type_ids.transpose(0, 1).contiguous()),
            None => None,
        };
        let attention_mask = match attention_mask {
            Some(attention_mask) => Some(attention_mask.transpose(0, 1).contiguous()),
            None => None,
        };
        let perm_mask = match perm_mask {
            Some(perm_mask) => Some(perm_mask.permute(&[1, 2, 0]).contiguous()),
            None => None,
        };
        let target_mapping = match target_mapping {
            Some(target_mapping) => Some(target_mapping.permute(&[1, 2, 0]).contiguous()),
            None => None,
        };

        let m_len = if let Some(mems) = &old_layer_states {
            if let Some(mem_0) = &mems[0] {
                mem_0.prev_content.size()[0]
            } else {
                0
            }
        } else {
            0
        };
        let (q_len, batch_size) = (input_shape[0], input_shape[1]);
        let k_len = q_len + m_len;

        let mut attn_mask = match self.attention_type {
            AttentionType::uni => Some(
                self.create_mask(q_len, m_len, word_emb_k.device())
                    .unsqueeze(-1)
                    .unsqueeze(-1),
            ),
            AttentionType::bi => None,
        };

        let input_mask: Option<Tensor> = if let Some(attention_mask) = attention_mask {
            Some(1.0 - attention_mask)
        } else {
            None
        };

        let mut data_mask: Option<Tensor> = match (input_mask, perm_mask) {
            (Some(input_mask_value), Some(perm_mask_value)) => {
                Some(input_mask_value.unsqueeze(0) + perm_mask_value)
            }
            (Some(input_mask_value), None) => Some(input_mask_value.unsqueeze(0)),
            (None, Some(perm_mask_value)) => Some(perm_mask_value),
            (None, None) => None,
        };

        if let Some(data_mask_value) = &data_mask {
            if m_len > 0 {
                let mems_mask = Tensor::zeros(
                    &[data_mask_value.size()[0], m_len, batch_size],
                    (Kind::Bool, data_mask_value.device()),
                );
                data_mask = Some(Tensor::cat(&[&mems_mask, data_mask_value], 1))
            }
            attn_mask = Some(if let Some(attn_mask) = attn_mask {
                attn_mask + data_mask.unwrap().unsqueeze(-1)
            } else {
                data_mask.unwrap().unsqueeze(-1)
            });
        }

        let non_tgt_mask = if let Some(attn_mask_value) = &attn_mask {
            let mut non_tgt_mask = -Tensor::eye(q_len, (Kind::Int64, attn_mask_value.device()));
            if m_len > 0 {
                non_tgt_mask = Tensor::cat(
                    &[
                        Tensor::zeros(&[q_len, m_len], (Kind::Int64, attn_mask_value.device())),
                        non_tgt_mask,
                    ],
                    -1,
                );
            }
            Some((attn_mask_value + non_tgt_mask.unsqueeze(-1).unsqueeze(-1)).gt(0))
        } else {
            None
        };

        let mut output_h = word_emb_k.apply_t(&self.dropout, train);
        let mut output_g = if let Some(target_mapping_value) = &target_mapping {
            Some(
                (&self
                    .mask_emb
                    .expand(&[target_mapping_value.size()[0], batch_size, -1], true))
                    .apply_t(&self.dropout, train),
            )
        } else {
            None
        };

        let seg_mat = if let Some(token_type_ids_value) = token_type_ids {
            let cat_ids = if m_len > 0 {
                let mem_pad = Tensor::zeros(
                    &[m_len, batch_size],
                    (Kind::Int64, token_type_ids_value.device()),
                );
                Tensor::cat(&[mem_pad, token_type_ids_value.copy()], 0)
            } else {
                token_type_ids_value.copy()
            };
            let seg_mat = token_type_ids_value
                .unsqueeze(-1)
                .ne1(&cat_ids.unsqueeze(0))
                .to_kind(Kind::Int64);
            Some(seg_mat.one_hot(2).to_kind(Kind::Float))
        } else {
            None
        };

        let pos_emb = self
            .relative_positional_encoding(q_len, k_len, Some(batch_size), output_h.device())
            .apply_t(&self.dropout, train);

        let mut all_hidden_states: Option<Vec<(Tensor, Option<Tensor>)>> =
            if self.output_hidden_states {
                Some(vec![])
            } else {
                None
            };
        let mut all_attentions: Option<Vec<(Tensor, Option<Tensor>)>> = if self.output_attentions {
            Some(vec![])
        } else {
            None
        };

        let mut next_cache: Option<Vec<Option<LayerState>>> = if self.use_cache {
            if old_layer_states.is_some() {
                old_layer_states
            } else {
                Some(vec![None; self.layers.len()])
            }
        } else {
            None
        };

        for (layer_idx, layer) in self.layers.iter().enumerate() {
            let layer_state = match &next_cache {
                Some(values) => values[layer_idx].to_owned(),
                None => None,
            };
            if let Some(next_cache_value) = next_cache.borrow_mut() {
                next_cache_value[layer_idx] = Some(self.cache_mem(&output_h, &layer_state));
            }
            let temp = layer.forward_t(
                &output_h,
                output_g.as_ref(),
                non_tgt_mask.as_ref(),
                attn_mask.as_ref(),
                &pos_emb,
                seg_mat.as_ref(),
                layer_state,
                target_mapping.as_ref(),
                train,
            );
            output_h = temp.0;
            output_g = temp.1;
            let attention_probas_h = temp.2;
            let attention_probas_g = temp.3;
            if let Some(hidden_states) = all_hidden_states.borrow_mut() {
                hidden_states.push((
                    output_h.copy(),
                    if let Some(output) = &output_g {
                        Some(output.copy())
                    } else {
                        None
                    },
                ));
            };
            if let Some(attentions) = all_attentions.borrow_mut() {
                attentions.push((attention_probas_h.unwrap(), attention_probas_g));
            };
        }
        let hidden_state = if let Some(output_g_value) = output_g {
            output_g_value
        } else {
            output_h
        }
        .apply_t(&self.dropout, train)
        .permute(&[1, 0, 2])
        .contiguous();

        Ok(XLNetModelOutput {
            hidden_state,
            next_cache,
            all_hidden_states,
            all_attentions,
        })
    }
}

/// # XLNetLMHeadModel

/// XLNet model with a language model head for language generation tasks

/// It is made of the following blocks:

/// - `base_model`: `XLNetModel`

/// - `lm_head`: Linear language modeling head, projecting the hidden state logits to the vocabulary space

pub struct XLNetLMHeadModel {
    base_model: XLNetModel,
    lm_head: nn::Linear,
}

impl XLNetLMHeadModel {
    /// Build a new `XLNetLMHeadModel`

    ///

    /// # Arguments

    ///

    /// * `p` - Variable store path for the root of the XLNet model

    /// * `config` - `XLNetConfig` object defining the model architecture

    ///

    /// # Example

    ///

    /// ```no_run

    /// use rust_bert::xlnet::{XLNetConfig, XLNetLMHeadModel};

    /// use rust_bert::Config;

    /// use std::path::Path;

    /// use tch::{nn, Device};

    ///

    /// let config_path = Path::new("path/to/config.json");

    /// let device = Device::Cpu;

    /// let p = nn::VarStore::new(device);

    /// let config = XLNetConfig::from_file(config_path);

    /// let xlnet_model = XLNetLMHeadModel::new(&p.root(), &config);

    /// ```

    pub fn new<'p, P>(p: P, config: &XLNetConfig) -> XLNetLMHeadModel
    where
        P: Borrow<nn::Path<'p>>,
    {
        let p = p.borrow();

        let base_model = XLNetModel::new(p / "transformer", config);
        let lm_head = nn::linear(
            p / "lm_loss",
            config.d_model,
            config.vocab_size,
            Default::default(),
        );

        XLNetLMHeadModel {
            base_model,
            lm_head,
        }
    }

    /// Forward pass through the model

    ///

    /// # Arguments

    ///

    /// * `input_ids` - Optional input tensor of shape (*batch size*, *sequence_length*). This or `input_embeds` must be provided.

    /// * `attention_mask` - Optional attention mask of shape (*batch size*, *sequence_length*) for the encoder positions. Positions with a mask with value 0 will be masked.

    /// * `perm_mask` - Optional tensor of shape (*batch size*, *sequence_length*, *sequence_length*). Mask to indicate the attention pattern for each input token (only used for pre-training over permutations, rather than simple token masking).

    /// * `target_mapping ` - Optional tensor of shape (*batch size*, *num_tokens*, *sequence_length*) indicating the position of the masked words to predict.

    /// * `token_type_ids` - Optional tensor (*batch size*, *sequence_length*) indicating the sentence ID of the token (0: first sentence, 1: second sentence).

    /// * `input_embeds` - Optional input tensor of shape (*batch size*, *sequence_length*, *embeddings dimension*). This or `input_ids` must be provided.

    /// * `old_layer_states` - Optional vector of length `num_layers` containing optional `LayerStates` containing the last calculated content for the attention layers. This avoids recomputing attention weights at past positions and speeds up decoding.

    /// * `train` - boolean flag to turn on/off the dropout layers in the model. Should be set to false for inference.

    ///

    /// # Returns

    ///

    /// * `LMModelOutput` containing:

    ///   - `lm_logits` - `Tensor` of shape (*batch size*, *sequence_length*, *vocab_size*) representing the logits for each vocab item and position

    ///   - `cache` - `XLNetCache` made of `Option<Vec<Option<LayerState>>>` of length *n_layers*  and shape (*past_sequence_length*, *batch size*, *hidden_size*) containing the previous content

    ///   - `encoder_hidden_states` - None

    ///   - `all_hidden_states` - `Option<Vec<Tensor>>` of length *n_layers* with shape (*batch size*, *sequence_length*, *hidden_size*)

    ///   - `all_attentions` - `Option<Vec<Tensor>>` of length *n_layers* with shape (*batch size*, *sequence_length*, *hidden_size*)

    ///

    /// # Example

    ///

    /// ```no_run

    /// # use tch::{nn, Device, Tensor, no_grad, Kind};

    /// # use rust_bert::Config;

    /// # use std::path::Path;

    /// # use tch::kind::Kind::{Int64, Double};

    /// use rust_bert::xlnet::{XLNetConfig, XLNetLMHeadModel};

    /// # let config_path = Path::new("path/to/config.json");

    /// # let vocab_path = Path::new("path/to/vocab.txt");

    /// # let device = Device::Cpu;

    /// # let vs = nn::VarStore::new(device);

    /// # let config = XLNetConfig::from_file(config_path);

    /// # let xlnet_model: XLNetLMHeadModel = XLNetLMHeadModel::new(&vs.root(), &config);

    /// let (batch_size, sequence_length) = (64, 128);

    /// let input_tensor = Tensor::rand(&[batch_size, sequence_length], (Int64, device));

    /// let attention_mask = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_tensor = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_mapping = Tensor::zeros(&[64, 1, 128], (Kind::Float, device));

    /// let _ = target_mapping.narrow(2, 3, 1).fill_(1.0);

    ///

    /// let model_output = no_grad(|| {

    ///     xlnet_model.forward_t(

    ///         Some(&input_tensor),

    ///         Some(&attention_mask),

    ///         None,

    ///         Some(&target_mapping),

    ///         None,

    ///         None,

    ///         None,

    ///         false,

    ///     )

    /// });

    /// ```

    pub fn forward_t(
        &self,
        input_ids: Option<&Tensor>,
        attention_mask: Option<&Tensor>,
        old_layer_states: Option<Vec<Option<LayerState>>>,
        perm_mask: Option<&Tensor>,
        target_mapping: Option<&Tensor>,
        token_type_ids: Option<&Tensor>,
        input_embeds: Option<Tensor>,
        train: bool,
    ) -> Result<LMModelOutput, RustBertError> {
        let base_model_output = self.base_model.forward_t(
            input_ids,
            attention_mask,
            old_layer_states,
            perm_mask,
            target_mapping,
            token_type_ids,
            input_embeds,
            train,
        )?;

        let lm_logits = base_model_output.hidden_state.apply(&self.lm_head);
        let all_attentions = if let Some(all_attentions) = base_model_output.all_attentions {
            Some(
                all_attentions
                    .into_iter()
                    .map(|(u, _)| u)
                    .collect::<Vec<Tensor>>(),
            )
        } else {
            None
        };
        let all_hidden_states = if let Some(all_hidden_states) = base_model_output.all_hidden_states
        {
            Some(
                all_hidden_states
                    .into_iter()
                    .map(|(u, _)| u)
                    .collect::<Vec<Tensor>>(),
            )
        } else {
            None
        };

        Ok(LMModelOutput {
            lm_logits,
            encoder_hidden_state: None,
            cache: Cache::XLNetCache(base_model_output.next_cache),
            all_hidden_states,
            all_attentions,
        })
    }
}

impl LMHeadModel for XLNetLMHeadModel {
    /// Forward pass through the model

    ///

    /// # Arguments

    ///

    /// * `input_ids` - Optional input tensor of shape (*batch size*, *sequence_length*). This or `input_embeds` must be provided.

    /// * `attention_mask` - Optional attention mask of shape (*batch size*, *sequence_length*) for the encoder positions. Positions with a mask with value 0 will be masked.

    /// * `perm_mask` - Optional tensor of shape (*batch size*, *sequence_length*, *sequence_length*). Mask to indicate the attention pattern for each input token (only used for pre-training over permutations, rather than simple token masking).

    /// * `target_mapping ` - Optional tensor of shape (*batch size*, *num_tokens*, *sequence_length*) indicating the position of the masked words to predict.

    /// * `token_type_ids` - Optional tensor (*batch size*, *sequence_length*) indicating the sentence ID of the token (0: first sentence, 1: second sentence).

    /// * `input_embeds` - Optional input tensor of shape (*batch size*, *sequence_length*, *embeddings dimension*). This or `input_ids` must be provided.

    /// * `old_layer_states` - Optional vector of length `num_layers` containing optional `LayerStates` containing the last calculated content for the attention layers. This avoids recomputing attention weights at past positions and speeds up decoding.

    /// * `train` - boolean flag to turn on/off the dropout layers in the model. Should be set to false for inference.

    ///

    /// # Returns

    ///

    /// * `LMModelOutput` containing:

    ///   - `lm_logits` - `Tensor` of shape (*batch size*, *sequence_length*, *vocab_size*) representing the logits for each vocab item and position

    ///   - `cache` - `XLNetCache` made of `Option<Vec<Option<LayerState>>>` of length *n_layers*  and shape (*past_sequence_length*, *batch size*, *hidden_size*) containing the previous content

    ///   - `encoder_hidden_states` - None

    ///   - `all_hidden_states` - `Option<Vec<Tensor>>` of length *n_layers* with shape (*batch size*, *sequence_length*, *hidden_size*)

    ///   - `all_attentions` - `Option<Vec<Tensor>>` of length *n_layers* with shape (*batch size*, *sequence_length*, *hidden_size*)

    ///

    /// # Example

    ///

    /// ```no_run

    /// # use tch::{nn, Device, Tensor, no_grad, Kind};

    /// # use rust_bert::Config;

    /// # use std::path::Path;

    /// # use tch::kind::Kind::{Int64, Double};

    /// use rust_bert::xlnet::{XLNetConfig, XLNetLMHeadModel};

    /// # let config_path = Path::new("path/to/config.json");

    /// # let vocab_path = Path::new("path/to/vocab.txt");

    /// # let device = Device::Cpu;

    /// # let vs = nn::VarStore::new(device);

    /// # let config = XLNetConfig::from_file(config_path);

    /// # let xlnet_model: XLNetLMHeadModel = XLNetLMHeadModel::new(&vs.root(), &config);

    /// let (batch_size, sequence_length) = (64, 128);

    /// let input_tensor = Tensor::rand(&[batch_size, sequence_length], (Int64, device));

    /// let attention_mask = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_tensor = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_mapping = Tensor::zeros(&[64, 1, 128], (Kind::Float, device));

    /// let _ = target_mapping.narrow(2, 3, 1).fill_(1.0);

    ///

    /// let model_output = no_grad(|| {

    ///     xlnet_model.forward_t(

    ///         Some(&input_tensor),

    ///         Some(&attention_mask),

    ///         None,

    ///         Some(&target_mapping),

    ///         None,

    ///         None,

    ///         None,

    ///         false,

    ///     )

    /// });

    /// ```

    fn forward_t(
        &self,
        input_ids: &Option<Tensor>,
        layer_past: Cache,
        attention_mask: &Option<Tensor>,
        _token_type_ids: &Option<Tensor>,
        _position_ids: &Option<Tensor>,
        _input_embeds: &Option<Tensor>,
        _encoder_outputs: Option<&Tensor>,
        decoder_input_ids: &Option<Tensor>,
        train: bool,
    ) -> Result<LMModelOutput, RustBertError> {
        match layer_past {
            Cache::XLNetCache(layer_past) => self.forward_t(
                input_ids.as_ref(),
                None,
                layer_past,
                attention_mask.as_ref(),
                // For XLNet the decoder_input_ids are used as a placeholder for the target mapping

                decoder_input_ids.as_ref(),
                None,
                None,
                train,
            ),
            Cache::None => self.forward_t(
                input_ids.as_ref(),
                None,
                None,
                attention_mask.as_ref(),
                // For XLNet the decoder_input_ids are used as a placeholder for the target mapping

                decoder_input_ids.as_ref(),
                None,
                None,
                train,
            ),
            _ => Err(RustBertError::ValueError(
                "Cache not compatible with XLNet Model".into(),
            )),
        }
    }
}

/// # XLNetForSequenceClassification

/// XLNet model with a classification head for sequence classification tasks

/// It is made of the following blocks:

/// - `base_model`: `XLNetModel`

/// - `sequence_summary`: `SequenceSummary` to pool the base model hidden states

/// - `logits_proj`: Linear layer projecting the hidden layer pooled output to the target space

pub struct XLNetForSequenceClassification {
    base_model: XLNetModel,
    sequence_summary: SequenceSummary,
    logits_proj: nn::Linear,
}

impl XLNetForSequenceClassification {
    /// Build a new `XLNetForSequenceClassification`

    ///

    /// # Arguments

    ///

    /// * `p` - Variable store path for the root of the XLNet model

    /// * `config` - `XLNetConfig` object defining the model architecture

    ///

    /// # Example

    ///

    /// ```no_run

    /// use rust_bert::xlnet::{XLNetConfig, XLNetForSequenceClassification};

    /// use rust_bert::Config;

    /// use std::path::Path;

    /// use tch::{nn, Device};

    ///

    /// let config_path = Path::new("path/to/config.json");

    /// let device = Device::Cpu;

    /// let p = nn::VarStore::new(device);

    /// let config = XLNetConfig::from_file(config_path);

    /// let xlnet_model = XLNetForSequenceClassification::new(&p.root(), &config);

    /// ```

    pub fn new<'p, P>(
        p: P,
        config: &XLNetConfig,
    ) -> Result<XLNetForSequenceClassification, RustBertError>
    where
        P: Borrow<nn::Path<'p>>,
    {
        let p = p.borrow();

        let base_model = XLNetModel::new(p / "transformer", config);
        let sequence_summary =
            SequenceSummary::new(p / "sequence_summary", &SummaryConfig::from(config))?;
        let num_labels = config
            .id2label
            .as_ref()
            .expect("num_labels not provided in configuration")
            .len() as i64;

        let logits_proj = nn::linear(
            p / "logits_proj",
            config.d_model,
            num_labels,
            Default::default(),
        );

        Ok(XLNetForSequenceClassification {
            base_model,
            sequence_summary,
            logits_proj,
        })
    }

    /// Forward pass through the model

    ///

    /// # Arguments

    ///

    /// * `input_ids` - Optional input tensor of shape (*batch size*, *sequence_length*). This or `input_embeds` must be provided.

    /// * `attention_mask` - Optional attention mask of shape (*batch size*, *sequence_length*) for the encoder positions. Positions with a mask with value 0 will be masked.

    /// * `perm_mask` - Optional tensor of shape (*batch size*, *sequence_length*, *sequence_length*). Mask to indicate the attention pattern for each input token (only used for pre-training over permutations, rather than simple token masking).

    /// * `target_mapping ` - Optional tensor of shape (*batch size*, *num_tokens*, *sequence_length*) indicating the position of the masked words to predict.

    /// * `token_type_ids` - Optional tensor (*batch size*, *sequence_length*) indicating the sentence ID of the token (0: first sentence, 1: second sentence).

    /// * `input_embeds` - Optional input tensor of shape (*batch size*, *sequence_length*, *embeddings dimension*). This or `input_ids` must be provided.

    /// * `old_layer_states` - Optional vector of length `num_layers` containing optional `LayerStates` containing the last calculated content for the attention layers. This avoids recomputing attention weights at past positions and speeds up decoding.

    /// * `train` - boolean flag to turn on/off the dropout layers in the model. Should be set to false for inference.

    ///

    /// # Returns

    ///

    /// * `XLNetSequenceClassificationOutput` containing:

    ///   - `logits` - `Tensor` of shape (*batch size*, *num_classes*) representing the logits for each batch item and class

    ///   - `next_cache` - `Option<Vec<Option<LayerState>>>` of length *n_layer* containing the past content for the the attention layers with shape (*past_sequence_length*, *batch size*, *hidden_size*)

    ///   - `all_hidden_states` - `Option<Vec<(Tensor, Option<Tensor>)>>` of length *n_layer* with shape (*batch size*, *sequence_length*, *hidden_size*) (with optional query stream states if used)

    ///   - `all_attentions` - `Option<Vec<(Tensor, Option<Tensor>)>>` of length *n_layer* with shape (*batch size*, *sequence_length*, *hidden_size*) (with optional query stream states if used)

    ///

    /// # Example

    ///

    /// ```no_run

    /// # use tch::{nn, Device, Tensor, no_grad, Kind};

    /// # use rust_bert::Config;

    /// # use std::path::Path;

    /// # use tch::kind::Kind::{Int64, Double};

    /// use rust_bert::xlnet::{XLNetConfig, XLNetForSequenceClassification};

    /// # fn main() -> anyhow::Result<()> {

    /// # let config_path = Path::new("path/to/config.json");

    /// # let vocab_path = Path::new("path/to/vocab.txt");

    /// # let device = Device::Cpu;

    /// # let vs = nn::VarStore::new(device);

    /// # let config = XLNetConfig::from_file(config_path);

    /// # let xlnet_model: XLNetForSequenceClassification = XLNetForSequenceClassification::new(&vs.root(), &config)?;

    /// let (batch_size, sequence_length) = (64, 128);

    /// let input_tensor = Tensor::rand(&[batch_size, sequence_length], (Int64, device));

    /// let attention_mask = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_tensor = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_mapping = Tensor::zeros(&[64, 1, 128], (Kind::Float, device));

    /// let _ = target_mapping.narrow(2, 3, 1).fill_(1.0);

    ///

    /// let model_output = no_grad(|| {

    ///     xlnet_model.forward_t(

    ///         Some(&input_tensor),

    ///         Some(&attention_mask),

    ///         None,

    ///         Some(&target_mapping),

    ///         None,

    ///         None,

    ///         None,

    ///         false

    ///     )

    /// });

    /// # Ok(())

    /// # }

    /// ```

    pub fn forward_t(
        &self,
        input_ids: Option<&Tensor>,
        attention_mask: Option<&Tensor>,
        old_layer_states: Option<Vec<Option<LayerState>>>,
        perm_mask: Option<&Tensor>,
        target_mapping: Option<&Tensor>,
        token_type_ids: Option<&Tensor>,
        input_embeds: Option<Tensor>,
        train: bool,
    ) -> XLNetSequenceClassificationOutput {
        let base_model_output = self
            .base_model
            .forward_t(
                input_ids,
                attention_mask,
                old_layer_states,
                perm_mask,
                target_mapping,
                token_type_ids,
                input_embeds,
                train,
            )
            .unwrap();

        let logits = self
            .sequence_summary
            .forward_t(&base_model_output.hidden_state, None, train)
            .apply(&self.logits_proj);

        XLNetSequenceClassificationOutput {
            logits,
            next_cache: base_model_output.next_cache,
            all_hidden_states: base_model_output.all_hidden_states,
            all_attentions: base_model_output.all_attentions,
        }
    }
}

/// # XLNetForTokenClassification

/// XLNet model with a classification head for token-level classification tasks

/// It is made of the following blocks:

/// - `base_model`: `XLNetModel`

/// - `classifier`: Linear layer projecting the hidden layer output to the target space

pub struct XLNetForTokenClassification {
    base_model: XLNetModel,
    classifier: nn::Linear,
}

impl XLNetForTokenClassification {
    /// Build a new `XLNetForTokenClassification`

    ///

    /// # Arguments

    ///

    /// * `p` - Variable store path for the root of the XLNet model

    /// * `config` - `XLNetConfig` object defining the model architecture

    ///

    /// # Example

    ///

    /// ```no_run

    /// use rust_bert::xlnet::{XLNetConfig, XLNetForTokenClassification};

    /// use rust_bert::Config;

    /// use std::path::Path;

    /// use tch::{nn, Device};

    ///

    /// let config_path = Path::new("path/to/config.json");

    /// let device = Device::Cpu;

    /// let p = nn::VarStore::new(device);

    /// let config = XLNetConfig::from_file(config_path);

    /// let xlnet_model = XLNetForTokenClassification::new(&p.root(), &config);

    /// ```

    pub fn new<'p, P>(
        p: P,
        config: &XLNetConfig,
    ) -> Result<XLNetForTokenClassification, RustBertError>
    where
        P: Borrow<nn::Path<'p>>,
    {
        let p = p.borrow();

        let base_model = XLNetModel::new(p / "transformer", config);
        let num_labels = config
            .id2label
            .as_ref()
            .expect("num_labels not provided in configuration")
            .len() as i64;

        let classifier = nn::linear(
            p / "classifier",
            config.d_model,
            num_labels,
            Default::default(),
        );

        Ok(XLNetForTokenClassification {
            base_model,
            classifier,
        })
    }

    /// Forward pass through the model

    ///

    /// # Arguments

    ///

    /// * `input_ids` - Optional input tensor of shape (*batch size*, *sequence_length*). This or `input_embeds` must be provided.

    /// * `attention_mask` - Optional attention mask of shape (*batch size*, *sequence_length*) for the encoder positions. Positions with a mask with value 0 will be masked.

    /// * `perm_mask` - Optional tensor of shape (*batch size*, *sequence_length*, *sequence_length*). Mask to indicate the attention pattern for each input token (only used for pre-training over permutations, rather than simple token masking).

    /// * `target_mapping ` - Optional tensor of shape (*batch size*, *num_tokens*, *sequence_length*) indicating the position of the masked words to predict.

    /// * `token_type_ids` - Optional tensor (*batch size*, *sequence_length*) indicating the sentence ID of the token (0: first sentence, 1: second sentence).

    /// * `input_embeds` - Optional input tensor of shape (*batch size*, *sequence_length*, *embeddings dimension*). This or `input_ids` must be provided.

    /// * `old_layer_states` - Optional vector of length `num_layers` containing optional `LayerStates` containing the last calculated content for the attention layers. This avoids recomputing attention weights at past positions and speeds up decoding.

    /// * `train` - boolean flag to turn on/off the dropout layers in the model. Should be set to false for inference.

    ///

    /// # Returns

    ///

    /// * `XLNetTokenClassificationOutput` containing:

    ///   - `logits` - `Tensor` of shape (*batch size*, *sequence_length*, *num_classes*) representing the logits for each batch item, token position and class

    ///   - `next_cache` - `Option<Vec<Option<LayerState>>>` of length *n_layer* containing the past content for the the attention layers with shape (*past_sequence_length*, *batch size*, *hidden_size*)

    ///   - `all_hidden_states` - `Option<Vec<(Tensor, Option<Tensor>)>>` of length *n_layer* with shape (*batch size*, *sequence_length*, *hidden_size*) (with optional query stream states if used)

    ///   - `all_attentions` - `Option<Vec<(Tensor, Option<Tensor>)>>` of length *n_layer* with shape (*batch size*, *sequence_length*, *hidden_size*) (with optional query stream states if used)

    ///

    /// # Example

    ///

    /// ```no_run

    /// # use tch::{nn, Device, Tensor, no_grad, Kind};

    /// # use rust_bert::Config;

    /// # use std::path::Path;

    /// # use tch::kind::Kind::{Int64, Double};

    /// use rust_bert::xlnet::{XLNetConfig, XLNetForTokenClassification};

    /// # fn main() -> anyhow::Result<()> {

    /// # let config_path = Path::new("path/to/config.json");

    /// # let vocab_path = Path::new("path/to/vocab.txt");

    /// # let device = Device::Cpu;

    /// # let vs = nn::VarStore::new(device);

    /// # let config = XLNetConfig::from_file(config_path);

    /// # let xlnet_model: XLNetForTokenClassification = XLNetForTokenClassification::new(&vs.root(), &config)?;

    /// let (batch_size, sequence_length) = (64, 128);

    /// let input_tensor = Tensor::rand(&[batch_size, sequence_length], (Int64, device));

    /// let attention_mask = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_tensor = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_mapping = Tensor::zeros(&[64, 1, 128], (Kind::Float, device));

    /// let _ = target_mapping.narrow(2, 3, 1).fill_(1.0);

    ///

    /// let model_output = no_grad(|| {

    ///     xlnet_model.forward_t(

    ///         Some(&input_tensor),

    ///         Some(&attention_mask),

    ///         None,

    ///         Some(&target_mapping),

    ///         None,

    ///         None,

    ///         None,

    ///         false

    ///     )

    /// });

    /// # Ok(())

    /// # }

    /// ```

    pub fn forward_t(
        &self,
        input_ids: Option<&Tensor>,
        attention_mask: Option<&Tensor>,
        old_layer_states: Option<Vec<Option<LayerState>>>,
        perm_mask: Option<&Tensor>,
        target_mapping: Option<&Tensor>,
        token_type_ids: Option<&Tensor>,
        input_embeds: Option<Tensor>,
        train: bool,
    ) -> XLNetTokenClassificationOutput {
        let base_model_output = self
            .base_model
            .forward_t(
                input_ids,
                attention_mask,
                old_layer_states,
                perm_mask,
                target_mapping,
                token_type_ids,
                input_embeds,
                train,
            )
            .unwrap();

        let logits = base_model_output.hidden_state.apply(&self.classifier);

        XLNetTokenClassificationOutput {
            logits,
            next_cache: base_model_output.next_cache,
            all_hidden_states: base_model_output.all_hidden_states,
            all_attentions: base_model_output.all_attentions,
        }
    }
}

/// # XLNetForMultipleChoice

/// Multiple choices model using a XLNet base model and a linear classifier.

/// Input should be in the form `[CLS] Context [SEP] Possible choice [SEP]`. The choice is made along the batch axis,

/// assuming all elements of the batch are alternatives to be chosen from for a given context.

/// It is made of the following blocks:

/// - `base_model`: `XLNetModel`

/// - `sequence_summary`: `SequenceSummary` to pool the base model hidden states

/// - `logits_proj`: Linear layer projecting the hidden layer pooled output to a single value

pub struct XLNetForMultipleChoice {
    base_model: XLNetModel,
    sequence_summary: SequenceSummary,
    logits_proj: nn::Linear,
}

impl XLNetForMultipleChoice {
    /// Build a new `XLNetForMultipleChoice`

    ///

    /// # Arguments

    ///

    /// * `p` - Variable store path for the root of the XLNet model

    /// * `config` - `XLNetConfig` object defining the model architecture

    ///

    /// # Example

    ///

    /// ```no_run

    /// use rust_bert::xlnet::{XLNetConfig, XLNetForMultipleChoice};

    /// use rust_bert::Config;

    /// use std::path::Path;

    /// use tch::{nn, Device};

    ///

    /// let config_path = Path::new("path/to/config.json");

    /// let device = Device::Cpu;

    /// let p = nn::VarStore::new(device);

    /// let config = XLNetConfig::from_file(config_path);

    /// let xlnet_model = XLNetForMultipleChoice::new(&p.root(), &config);

    /// ```

    pub fn new<'p, P>(p: P, config: &XLNetConfig) -> Result<XLNetForMultipleChoice, RustBertError>
    where
        P: Borrow<nn::Path<'p>>,
    {
        let p = p.borrow();

        let base_model = XLNetModel::new(p / "transformer", config);
        let sequence_summary =
            SequenceSummary::new(p / "sequence_summary", &SummaryConfig::from(config))?;

        let logits_proj = nn::linear(p / "logits_proj", config.d_model, 1, Default::default());

        Ok(XLNetForMultipleChoice {
            base_model,
            sequence_summary,
            logits_proj,
        })
    }

    /// Forward pass through the model

    ///

    /// # Arguments

    ///

    /// * `input_ids` - Optional input tensor of shape (*batch size*, *sequence_length*). This or `input_embeds` must be provided.

    /// * `attention_mask` - Optional attention mask of shape (*batch size*, *sequence_length*) for the encoder positions. Positions with a mask with value 0 will be masked.

    /// * `perm_mask` - Optional tensor of shape (*batch size*, *sequence_length*, *sequence_length*). Mask to indicate the attention pattern for each input token (only used for pre-training over permutations, rather than simple token masking).

    /// * `target_mapping ` - Optional tensor of shape (*batch size*, *num_tokens*, *sequence_length*) indicating the position of the masked words to predict.

    /// * `token_type_ids` - Optional tensor (*batch size*, *sequence_length*) indicating the sentence ID of the token (0: first sentence, 1: second sentence).

    /// * `input_embeds` - Optional input tensor of shape (*batch size*, *sequence_length*, *embeddings dimension*). This or `input_ids` must be provided.

    /// * `old_layer_states` - Optional vector of length `num_layers` containing optional `LayerStates` containing the last calculated content for the attention layers. This avoids recomputing attention weights at past positions and speeds up decoding.

    /// * `train` - boolean flag to turn on/off the dropout layers in the model. Should be set to false for inference.

    ///

    /// # Returns

    ///

    /// * `XLNetSequenceClassificationOutput` containing:

    ///   - `logits` - `Tensor` of shape (*1*, *batch size*) containing the logits for each of the alternatives given

    ///   - `next_cache` - `Option<Vec<Option<LayerState>>>` of length *n_layer* containing the past content for the the attention layers with shape (*past_sequence_length*, *batch size*, *hidden_size*)

    ///   - `all_hidden_states` - `Option<Vec<(Tensor, Option<Tensor>)>>` of length *n_layer* with shape (*batch size*, *sequence_length*, *hidden_size*) (with optional query stream states if used)

    ///   - `all_attentions` - `Option<Vec<(Tensor, Option<Tensor>)>>` of length *n_layer* with shape (*batch size*, *sequence_length*, *hidden_size*) (with optional query stream states if used)

    ///

    /// # Example

    ///

    /// ```no_run

    /// # use tch::{nn, Device, Tensor, no_grad, Kind};

    /// # use rust_bert::Config;

    /// # use std::path::Path;

    /// # use tch::kind::Kind::{Int64, Double};

    /// use rust_bert::xlnet::{XLNetConfig, XLNetForMultipleChoice};

    /// # fn main() -> anyhow::Result<()> {

    /// # let config_path = Path::new("path/to/config.json");

    /// # let vocab_path = Path::new("path/to/vocab.txt");

    /// # let device = Device::Cpu;

    /// # let vs = nn::VarStore::new(device);

    /// # let config = XLNetConfig::from_file(config_path);

    /// # let xlnet_model: XLNetForMultipleChoice = XLNetForMultipleChoice::new(&vs.root(), &config)?;

    /// let (batch_size, sequence_length) = (64, 128);

    /// let input_tensor = Tensor::rand(&[batch_size, sequence_length], (Int64, device));

    /// let attention_mask = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_tensor = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_mapping = Tensor::zeros(&[64, 1, 128], (Kind::Float, device));

    /// let _ = target_mapping.narrow(2, 3, 1).fill_(1.0);

    ///

    /// let model_output = no_grad(|| {

    ///     xlnet_model.forward_t(

    ///         Some(&input_tensor),

    ///         Some(&attention_mask),

    ///         None,

    ///         Some(&target_mapping),

    ///         None,

    ///         None,

    ///         None,

    ///         false

    ///     )

    /// });

    /// # Ok(())

    /// # }

    /// ```

    pub fn forward_t(
        &self,
        input_ids: Option<&Tensor>,
        attention_mask: Option<&Tensor>,
        old_layer_states: Option<Vec<Option<LayerState>>>,
        perm_mask: Option<&Tensor>,
        target_mapping: Option<&Tensor>,
        token_type_ids: Option<&Tensor>,
        input_embeds: Option<Tensor>,
        train: bool,
    ) -> XLNetSequenceClassificationOutput {
        let (input_ids, num_choices) = match input_ids {
            Some(value) => (
                Some(value.view((-1, *value.size().last().unwrap()))),
                value.size()[1],
            ),
            None => (
                None,
                input_embeds
                    .as_ref()
                    .expect("At least one of input ids or input_embeds must be provided")
                    .size()[1],
            ),
        };

        let attention_mask = match attention_mask {
            Some(value) => Some(value.view((-1, *value.size().last().unwrap()))),
            None => None,
        };
        let token_type_ids = match token_type_ids {
            Some(value) => Some(value.view((-1, *value.size().last().unwrap()))),
            None => None,
        };
        let input_embeds = match input_embeds {
            Some(value) => Some(value.view((-1, value.size()[1], value.size()[2]))),
            None => None,
        };
        let base_model_output = self
            .base_model
            .forward_t(
                input_ids.as_ref(),
                attention_mask.as_ref(),
                old_layer_states,
                perm_mask,
                target_mapping,
                token_type_ids.as_ref(),
                input_embeds,
                train,
            )
            .unwrap();

        let logits = self
            .sequence_summary
            .forward_t(&base_model_output.hidden_state, None, train)
            .apply(&self.logits_proj)
            .view((-1, num_choices));

        XLNetSequenceClassificationOutput {
            logits,
            next_cache: base_model_output.next_cache,
            all_hidden_states: base_model_output.all_hidden_states,
            all_attentions: base_model_output.all_attentions,
        }
    }
}

/// # XLNet for question answering

/// Extractive question-answering model based on a XLNet language model. Identifies the segment of a context that answers a provided question.

/// Please note that a significant amount of pre- and post-processing is required to perform end-to-end question answering.

/// See the question answering pipeline (also provided in this crate) for more details.

/// It is made of the following blocks:

/// - `base_model`: Base `XLNetModel`

/// - `qa_outputs`: Linear layer for question answering

pub struct XLNetForQuestionAnswering {
    base_model: XLNetModel,
    qa_outputs: nn::Linear,
}

impl XLNetForQuestionAnswering {
    /// Build a new `XLNetForQuestionAnswering`

    ///

    /// # Arguments

    ///

    /// * `p` - Variable store path for the root of the XLNet model

    /// * `config` - `XLNetConfig` object defining the model architecture

    ///

    /// # Example

    ///

    /// ```no_run

    /// use rust_bert::xlnet::{XLNetConfig, XLNetForQuestionAnswering};

    /// use rust_bert::Config;

    /// use std::path::Path;

    /// use tch::{nn, Device};

    ///

    /// let config_path = Path::new("path/to/config.json");

    /// let device = Device::Cpu;

    /// let p = nn::VarStore::new(device);

    /// let config = XLNetConfig::from_file(config_path);

    /// let xlnet_model = XLNetForQuestionAnswering::new(&p.root(), &config);

    /// ```

    pub fn new<'p, P>(
        p: P,
        config: &XLNetConfig,
    ) -> Result<XLNetForQuestionAnswering, RustBertError>
    where
        P: Borrow<nn::Path<'p>>,
    {
        let p = p.borrow();

        let base_model = XLNetModel::new(p / "transformer", config);
        let qa_outputs = nn::linear(p / "qa_outputs", config.d_model, 2, Default::default());

        Ok(XLNetForQuestionAnswering {
            base_model,
            qa_outputs,
        })
    }

    /// Forward pass through the model

    ///

    /// # Arguments

    ///

    /// * `input_ids` - Optional input tensor of shape (*batch size*, *sequence_length*). This or `input_embeds` must be provided.

    /// * `attention_mask` - Optional attention mask of shape (*batch size*, *sequence_length*) for the encoder positions. Positions with a mask with value 0 will be masked.

    /// * `perm_mask` - Optional tensor of shape (*batch size*, *sequence_length*, *sequence_length*). Mask to indicate the attention pattern for each input token (only used for pre-training over permutations, rather than simple token masking).

    /// * `target_mapping ` - Optional tensor of shape (*batch size*, *num_tokens*, *sequence_length*) indicating the position of the masked words to predict.

    /// * `token_type_ids` - Optional tensor (*batch size*, *sequence_length*) indicating the sentence ID of the token (0: first sentence, 1: second sentence).

    /// * `input_embeds` - Optional input tensor of shape (*batch size*, *sequence_length*, *embeddings dimension*). This or `input_ids` must be provided.

    /// * `old_layer_states` - Optional vector of length `num_layers` containing optional `LayerStates` containing the last calculated content for the attention layers. This avoids recomputing attention weights at past positions and speeds up decoding.

    /// * `train` - boolean flag to turn on/off the dropout layers in the model. Should be set to false for inference.

    ///

    /// # Returns

    ///

    /// * `XLNetQuestionAnsweringOutput` containing:

    ///   - `start_logits` - `Tensor` of shape (*batch size*, *sequence_length*) containing the logits for start of the answer

    ///   - `end_logits` - `Tensor` of shape (*batch size*, *sequence_length*) containing the logits for end of the answer

    ///   - `next_cache` - `Option<Vec<Option<LayerState>>>` of length *n_layer* containing the past content for the the attention layers with shape (*past_sequence_length*, *batch size*, *hidden_size*)

    ///   - `all_hidden_states` - `Option<Vec<(Tensor, Option<Tensor>)>>` of length *n_layer* with shape (*batch size*, *sequence_length*, *hidden_size*) (with optional query stream states if used)

    ///   - `all_attentions` - `Option<Vec<(Tensor, Option<Tensor>)>>` of length *n_layer* with shape (*batch size*, *sequence_length*, *hidden_size*) (with optional query stream states if used)

    ///

    /// # Example

    ///

    /// ```no_run

    /// # use tch::{nn, Device, Tensor, no_grad, Kind};

    /// # use rust_bert::Config;

    /// # use std::path::Path;

    /// # use tch::kind::Kind::{Int64, Double};

    /// use rust_bert::xlnet::{XLNetConfig, XLNetForMultipleChoice};

    /// # fn main() -> anyhow::Result<()> {

    /// # let config_path = Path::new("path/to/config.json");

    /// # let vocab_path = Path::new("path/to/vocab.txt");

    /// # let device = Device::Cpu;

    /// # let vs = nn::VarStore::new(device);

    /// # let config = XLNetConfig::from_file(config_path);

    /// # let xlnet_model: XLNetForMultipleChoice = XLNetForMultipleChoice::new(&vs.root(), &config)?;

    /// let (batch_size, sequence_length) = (64, 128);

    /// let input_tensor = Tensor::rand(&[batch_size, sequence_length], (Int64, device));

    /// let attention_mask = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_tensor = Tensor::ones(&[batch_size, sequence_length], (Int64, device));

    /// let target_mapping = Tensor::zeros(&[64, 1, 128], (Kind::Float, device));

    /// let _ = target_mapping.narrow(2, 3, 1).fill_(1.0);

    ///

    /// let model_output = no_grad(|| {

    ///     xlnet_model.forward_t(

    ///         Some(&input_tensor),

    ///         Some(&attention_mask),

    ///         None,

    ///         Some(&target_mapping),

    ///         None,

    ///         None,

    ///         None,

    ///         false

    ///     )

    /// });

    /// # Ok(())

    /// # }

    /// ```

    pub fn forward_t(
        &self,
        input_ids: Option<&Tensor>,
        attention_mask: Option<&Tensor>,
        old_layer_states: Option<Vec<Option<LayerState>>>,
        perm_mask: Option<&Tensor>,
        target_mapping: Option<&Tensor>,
        token_type_ids: Option<&Tensor>,
        input_embeds: Option<Tensor>,
        train: bool,
    ) -> XLNetQuestionAnsweringOutput {
        let base_model_output = self
            .base_model
            .forward_t(
                input_ids,
                attention_mask,
                old_layer_states,
                perm_mask,
                target_mapping,
                token_type_ids,
                input_embeds,
                train,
            )
            .unwrap();

        let sequence_output = base_model_output.hidden_state.apply(&self.qa_outputs);
        let logits = sequence_output.split(1, -1);
        let (start_logits, end_logits) = (&logits[0], &logits[1]);
        let start_logits = start_logits.squeeze1(-1);
        let end_logits = end_logits.squeeze1(-1);

        XLNetQuestionAnsweringOutput {
            start_logits,
            end_logits,
            next_cache: base_model_output.next_cache,
            all_hidden_states: base_model_output.all_hidden_states,
            all_attentions: base_model_output.all_attentions,
        }
    }
}

/// Container for the XLNet model output.

pub struct XLNetModelOutput {
    /// Last hidden states from the model

    pub hidden_state: Tensor,
    /// Cached hiden layer states for generation tasks

    pub next_cache: Option<Vec<Option<LayerState>>>,
    /// Hidden states for all intermediate layers

    pub all_hidden_states: Option<Vec<(Tensor, Option<Tensor>)>>,
    /// Attention weights for all intermediate layers

    pub all_attentions: Option<Vec<(Tensor, Option<Tensor>)>>,
}

/// Container for the XLNet sequence classification model output.

pub struct XLNetSequenceClassificationOutput {
    /// Last hidden states from the model

    pub logits: Tensor,
    /// Cached hiden layer states for generation tasks

    pub next_cache: Option<Vec<Option<LayerState>>>,
    /// Hidden states for all intermediate layers

    pub all_hidden_states: Option<Vec<(Tensor, Option<Tensor>)>>,
    /// Attention weights for all intermediate layers

    pub all_attentions: Option<Vec<(Tensor, Option<Tensor>)>>,
}

/// Container for the XLNet token classification model output.

pub struct XLNetTokenClassificationOutput {
    /// Last hidden states from the model

    pub logits: Tensor,
    /// Cached hiden layer states for generation tasks

    pub next_cache: Option<Vec<Option<LayerState>>>,
    /// Hidden states for all intermediate layers

    pub all_hidden_states: Option<Vec<(Tensor, Option<Tensor>)>>,
    /// Attention weights for all intermediate layers

    pub all_attentions: Option<Vec<(Tensor, Option<Tensor>)>>,
}

/// Container for the XLNet question answering model output.

pub struct XLNetQuestionAnsweringOutput {
    /// Logits for the start position for token of each input sequence

    pub start_logits: Tensor,
    /// Logits for the end position for token of each input sequence

    pub end_logits: Tensor,
    /// Cached hiden layer states for generation tasks

    pub next_cache: Option<Vec<Option<LayerState>>>,
    /// Hidden states for all intermediate layers

    pub all_hidden_states: Option<Vec<(Tensor, Option<Tensor>)>>,
    /// Attention weights for all intermediate layers

    pub all_attentions: Option<Vec<(Tensor, Option<Tensor>)>>,
}