-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extracting data for doc-string training
- Loading branch information
Showing
4 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import TrainingData.Frontend | ||
import TrainingData.InfoTree.ToJson | ||
import TrainingData.InfoTree.TacticInvocation.Basic | ||
import Mathlib.Data.String.Defs | ||
import Mathlib.Lean.CoreM | ||
import Std.Lean.Util.Path | ||
import Std.Data.String.Basic | ||
import Cli | ||
|
||
open Lean Elab IO Meta | ||
open Cli System | ||
|
||
structure CommentData where | ||
declName : String | ||
declType : String | ||
docString : Option String | ||
context : String | ||
decl : String | ||
deriving ToJson, FromJson | ||
|
||
def addStep (data : String × List CommentData) (step : CompilationStep) : | ||
IO (String × List CommentData) := do | ||
let (context, prev) := data | ||
let decl := step.src.toString | ||
let context := context ++ decl | ||
let new ← step.newDecls | ||
let next := new.map fun d => | ||
{ context | ||
docString := d.docString | ||
declName := d.name.toString | ||
declType := d.ppType | ||
decl } | ||
return (context, next ++ prev) | ||
|
||
def commentData (args : Cli.Parsed) : IO UInt32 := do | ||
searchPathRef.set compile_time_search_path% | ||
let module := args.positionalArg! "module" |>.as! ModuleName | ||
let steps ← compileModule module | ||
let (_, data) ← steps.foldlM (init := ("", [])) addStep | ||
IO.println <| toJson data | ||
return 0 | ||
|
||
/-- Setting up command line options and help text for `lake exe comment_data`. -/ | ||
def comment_data : Cmd := `[Cli| | ||
comment_data VIA commentData; ["0.0.1"] | ||
"Export doc-string training data from the given file. | ||
The output is a json array of dictionaries with fields | ||
* `declName`: the declaration name | ||
* `declType`: the pretty-printed type of the declaration | ||
* `docString`: the declaration doc-string, if it is present | ||
* `decl`: the entire body of the declaration | ||
* `context`: the file source up to before the declaration | ||
" | ||
|
||
ARGS: | ||
module : ModuleName; "Lean module to compile and export training data." | ||
] | ||
|
||
/-- `lake exe comment_data` -/ | ||
def main (args : List String) : IO UInt32 := | ||
comment_data.validate args | ||
|
||
-- See `scripts/comment_data.sh` for a script to run this on all of Mathlib. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env bash | ||
|
||
## Extracts doc-strings for every declaration. | ||
|
||
# See the help text in `lake exe comment_data` for a description of the output format. | ||
|
||
# Run either as `scripts/comment_data.sh` to run on all of Mathlib (several hours), | ||
# or `scripts/comment_data.sh Mathlib.Logic.Hydra` to run on just one file. | ||
# Results will go in `out/comment_data/Mathlib.Logic.Hydra.comments`. | ||
|
||
FLAGS=() | ||
ARGS=() | ||
|
||
for arg in "$@"; do | ||
if [[ $arg == --* ]]; then | ||
FLAGS+=("$arg") | ||
else | ||
ARGS+=("$arg") | ||
fi | ||
done | ||
|
||
if [ ${#ARGS[@]} -eq 0 ]; then | ||
lake build comment_data | ||
parallel -j32 ./scripts/comment_data.sh ${FLAGS[@]} -- ::: `cat .lake/packages/mathlib/Mathlib.lean | sed -e 's/import //'` | ||
else | ||
DIR=out/comment_data | ||
mkdir -p $DIR | ||
mod=${ARGS[0]} | ||
if [ ! -f $DIR/$mod.comment ]; then | ||
echo $mod | ||
if [ ! -f .lake/build/bin/comment_data ]; then | ||
lake build comment_data | ||
fi | ||
.lake/build/bin/comment_data ${FLAGS[@]} $mod > $DIR/$mod._comment && mv $DIR/$mod._comment $DIR/$mod.comment | ||
fi | ||
fi |