coastalcph/lexlms

LeXFiles and LegalLAMA: Facilitating English Multinational Legal Language Model Development

Python

23

15 commits

updated Jul 24, 2023

See the code

README

LeXFiles and LegalLAMA: Facilitating English Multinational Legal Language Model Development :balance_scale: :woman_judge: :world_map:

Introduction

Citation

Ilias Chalkidis*, Nicolas Garneau*, Catalina E.C. Goanta, Daniel Martin Katz, and Anders Søgaard. LeXFiles and LegalLAMA: Facilitating English Multinational Legal Language Model Development. 2022. In the Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics. Toronto, Canada.

@inproceedings{chalkidis-etal-2023-lexfiles,
    title = "{L}e{XF}iles and {L}egal{LAMA}: Facilitating {E}nglish Multinational Legal Language Model Development",
    author = "Chalkidis, Ilias  and
      Garneau, Nicolas  and
      Goanta, Catalina  and
      Katz, Daniel  and
      S{\o}gaard, Anders",
    booktitle = "Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)",
    month = jul,
    year = "2023",
    address = "Toronto, Canada",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2023.acl-long.865",
    pages = "15513--15535"
}

The LexFiles Corpus

The LeXFiles is a new diverse English multinational legal corpus that we created including 11 distinct sub-corpora that cover legislation and case law from 6 primarily English-speaking legal systems (EU, CoE, Canada, US, UK, India). The corpus contains approx. 19 billion tokens. In comparison, the "Pile of Law" corpus released by Hendersons et al. (2022) comprises 32 billion in total, where the majority (26/30) of sub-corpora come from the United States of America (USA), hence the corpus as a whole is biased towards the US legal system in general, and the federal or state jurisdiction in particular, to a significant extent.

Usage

from datasets import load_dataset
dataset = load_dataset('lexlms/lex_files', name='eu-legislation')

Dataset Specifications

The LexFiles

CorpusCorpus aliasDocumentsTokensPct.Sampl. (a=0.5)Sampl. (a=0.2)
:eu: EU Legislationeu-legislation93.7K233.7M1.2%5.0%8.0%
:eu: EU Court Decisionseu-court-cases29.8K178.5M0.9%4.3%7.6%
:eu: ECtHR Decisionsecthr-cases12.5K78.5M0.4%2.9%6.5%
:uk: UK Legislationuk-legislation52.5K143.6M0.7%3.9%7.3%
:uk: UK Court Decisionsuk-court-cases47K368.4M1.9%6.2%8.8%
:india: Indian Court Decisionsindian-court-cases34.8K111.6M0.6%3.4%6.9%
:canada: Canadian Legislationcanada-legislation6K33.5M0.2%1.9%5.5%
:canada: Canadian Court Decisionscanadian_decisions11.3K33.1M0.2%1.8%5.4%
:us: U.S. Court Decisions [1]us-court-cases4.6M11.4B59.2%34.7%17.5%
:us: U.S. Legislationus-legislation5181.4B7.4%12.3%11.5%
:us: U.S. Contractsus-contracts622K5.3B27.3%23.6%15.0%
Totallexlms/lex_files5.8M18.8B100%100%100%

[1] We consider only U.S. Court Decisions from 1965 onwards (cf. post Civil Rights Act), as a hard threshold for cases relying on severely out-dated and in many cases harmful law standards. The rest of the corpora include more recent documents.

[2] Sampling (Sampl.) ratios are computed following the exponential sampling introduced by Lample et al. (2019).

Additional corpora not considered for pre-training, since they do not represent factual legal knowledge.

CorpusCorpus aliasDocumentsTokens
:world_map: Legal web pages from C4legal-c4284K340M

LegalLAMA is a diverse probing benchmark suite comprising 8 sub-tasks that aims to assess the acquaintance of legal knowledge that PLMs acquired in pre-training.

Usage

from datasets import load_dataset
dataset = load_dataset('lexlms/legal_lama', name='contract_sections')

Dataset Specifications

CorpusCorpus aliasExamplesAvg. TokensLabels
:canada: Criminal Code Sections (Canada)canadian_sections32172144
:eu: Legal Terminology (EU)cjeu_term2,12716423
:us: Contractual Section Titles (US)contract_sections1,5278520
:us: Contract Types (US)contract_types1,08915015
:eu: ECHR Articles (CoE)ecthr_articles5,0726913
:eu: Legal Terminology (CoE)ecthr_terms6,80397250
:us: Crime Charges (US)us_crimes4,51811859
:us: Legal Terminology (US)us_terms5,82930892

Evaluating PLMs

We considered the following PLMs in our experimentations;

  • RoBERTa
  • LegalBERT
  • CaseLawBERT
  • PoL-BERT
  • LexLM

To evaluate these PLMs on LegalLAMA, run the following script:

sh scripts_lama/run_lama.sh

LexLMs - Pre-trained Language Models (PLMs)

We release 2 new legal-oriented PLMs, dubbed LexLMs, warm-started from the RoBERTa models, and further pre-trained on the "LeXFiles" corpuss for 1M additional steps.

Model NameAliasLayersHidden UnitsAttention HeadsParameters
Lex-LM (Base)lexlms/legal-roberta-base1276812123.9M
Lex-LM (Large)lexlms/legal-roberta-large24102416354.0M

Usage

You can load any model with the standard HF AutoModel code.

from transformers import AutoModel, AutoTokenizer

model = AutoModel.from_pretrained("lexlms/legal-roberta-base")
tokenizer = AutoTokenizer.from_pretrained("lexlms/legal-roberta-base")

Train Language Models

Train Tokenizer

Initially, we train a custom BPE tokenizer:

python train_tokenizer.py

Train Language Model

Then, we train an LM from scratch with Masked Language Modelling (MLM):

sh scripts_mlm/train_lm.sh

Model type (architecture, size) and other training specifications can be modified in the script:

MODEL_PATH='lexlm-large'
MODEL_MAX_LENGTH=512
TOTAL_STEPS=1000000
BATCH_SIZE=512

Fine-tune Language Models

Fine-tune on LexGLUE tasks

For example to fine-tune a PLM on EURLEX, you have to run:

sh lex-glue/scripts/run_eurlex.sh

PLM and other training specifications can be modified in the script:

MODEL_PATH='lexlms/legal-roberta-large'
legalnlp
legaltech
llms
nlproc

Contributors

ngarneau

8 commits

coastalcph/lexlms

LeXFiles and LegalLAMA: Facilitating English Multinational Legal Language Model Development

Python

23

15 commits

updated Jul 24, 2023

See the code

README

LeXFiles and LegalLAMA: Facilitating English Multinational Legal Language Model Development :balance_scale: :woman_judge: :world_map:

Introduction

Citation

Ilias Chalkidis*, Nicolas Garneau*, Catalina E.C. Goanta, Daniel Martin Katz, and Anders Søgaard. LeXFiles and LegalLAMA: Facilitating English Multinational Legal Language Model Development. 2022. In the Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics. Toronto, Canada.

@inproceedings{chalkidis-etal-2023-lexfiles,
    title = "{L}e{XF}iles and {L}egal{LAMA}: Facilitating {E}nglish Multinational Legal Language Model Development",
    author = "Chalkidis, Ilias  and
      Garneau, Nicolas  and
      Goanta, Catalina  and
      Katz, Daniel  and
      S{\o}gaard, Anders",
    booktitle = "Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)",
    month = jul,
    year = "2023",
    address = "Toronto, Canada",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2023.acl-long.865",
    pages = "15513--15535"
}

The LexFiles Corpus

The LeXFiles is a new diverse English multinational legal corpus that we created including 11 distinct sub-corpora that cover legislation and case law from 6 primarily English-speaking legal systems (EU, CoE, Canada, US, UK, India). The corpus contains approx. 19 billion tokens. In comparison, the "Pile of Law" corpus released by Hendersons et al. (2022) comprises 32 billion in total, where the majority (26/30) of sub-corpora come from the United States of America (USA), hence the corpus as a whole is biased towards the US legal system in general, and the federal or state jurisdiction in particular, to a significant extent.

Usage

from datasets import load_dataset
dataset = load_dataset('lexlms/lex_files', name='eu-legislation')

Dataset Specifications

The LexFiles

CorpusCorpus aliasDocumentsTokensPct.Sampl. (a=0.5)Sampl. (a=0.2)
:eu: EU Legislationeu-legislation93.7K233.7M1.2%5.0%8.0%
:eu: EU Court Decisionseu-court-cases29.8K178.5M0.9%4.3%7.6%
:eu: ECtHR Decisionsecthr-cases12.5K78.5M0.4%2.9%6.5%
:uk: UK Legislationuk-legislation52.5K143.6M0.7%3.9%7.3%
:uk: UK Court Decisionsuk-court-cases47K368.4M1.9%6.2%8.8%
:india: Indian Court Decisionsindian-court-cases34.8K111.6M0.6%3.4%6.9%
:canada: Canadian Legislationcanada-legislation6K33.5M0.2%1.9%5.5%
:canada: Canadian Court Decisionscanadian_decisions11.3K33.1M0.2%1.8%5.4%
:us: U.S. Court Decisions [1]us-court-cases4.6M11.4B59.2%34.7%17.5%
:us: U.S. Legislationus-legislation5181.4B7.4%12.3%11.5%
:us: U.S. Contractsus-contracts622K5.3B27.3%23.6%15.0%
Totallexlms/lex_files5.8M18.8B100%100%100%

[1] We consider only U.S. Court Decisions from 1965 onwards (cf. post Civil Rights Act), as a hard threshold for cases relying on severely out-dated and in many cases harmful law standards. The rest of the corpora include more recent documents.

[2] Sampling (Sampl.) ratios are computed following the exponential sampling introduced by Lample et al. (2019).

Additional corpora not considered for pre-training, since they do not represent factual legal knowledge.

CorpusCorpus aliasDocumentsTokens
:world_map: Legal web pages from C4legal-c4284K340M

LegalLAMA is a diverse probing benchmark suite comprising 8 sub-tasks that aims to assess the acquaintance of legal knowledge that PLMs acquired in pre-training.

Usage

from datasets import load_dataset
dataset = load_dataset('lexlms/legal_lama', name='contract_sections')

Dataset Specifications

CorpusCorpus aliasExamplesAvg. TokensLabels
:canada: Criminal Code Sections (Canada)canadian_sections32172144
:eu: Legal Terminology (EU)cjeu_term2,12716423
:us: Contractual Section Titles (US)contract_sections1,5278520
:us: Contract Types (US)contract_types1,08915015
:eu: ECHR Articles (CoE)ecthr_articles5,0726913
:eu: Legal Terminology (CoE)ecthr_terms6,80397250
:us: Crime Charges (US)us_crimes4,51811859
:us: Legal Terminology (US)us_terms5,82930892

Evaluating PLMs

We considered the following PLMs in our experimentations;

  • RoBERTa
  • LegalBERT
  • CaseLawBERT
  • PoL-BERT
  • LexLM

To evaluate these PLMs on LegalLAMA, run the following script:

sh scripts_lama/run_lama.sh

LexLMs - Pre-trained Language Models (PLMs)

We release 2 new legal-oriented PLMs, dubbed LexLMs, warm-started from the RoBERTa models, and further pre-trained on the "LeXFiles" corpuss for 1M additional steps.

Model NameAliasLayersHidden UnitsAttention HeadsParameters
Lex-LM (Base)lexlms/legal-roberta-base1276812123.9M
Lex-LM (Large)lexlms/legal-roberta-large24102416354.0M

Usage

You can load any model with the standard HF AutoModel code.

from transformers import AutoModel, AutoTokenizer

model = AutoModel.from_pretrained("lexlms/legal-roberta-base")
tokenizer = AutoTokenizer.from_pretrained("lexlms/legal-roberta-base")

Train Language Models

Train Tokenizer

Initially, we train a custom BPE tokenizer:

python train_tokenizer.py

Train Language Model

Then, we train an LM from scratch with Masked Language Modelling (MLM):

sh scripts_mlm/train_lm.sh

Model type (architecture, size) and other training specifications can be modified in the script:

MODEL_PATH='lexlm-large'
MODEL_MAX_LENGTH=512
TOTAL_STEPS=1000000
BATCH_SIZE=512

Fine-tune Language Models

Fine-tune on LexGLUE tasks

For example to fine-tune a PLM on EURLEX, you have to run:

sh lex-glue/scripts/run_eurlex.sh

PLM and other training specifications can be modified in the script:

MODEL_PATH='lexlms/legal-roberta-large'
legalnlp
legaltech
llms
nlproc

Contributors

ngarneau

8 commits

Languages

Python

97.8%

Shell

2.2%