Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03.1k
1/* eslint no-console:0 */2/**3 * This is a module for storing settings passed into KaTeX. It correctly handles4 * default settings.5 */6 7import {protocolFromUrl} from "./utils";8import ParseError from "./ParseError";9import {Token} from "./Token";10 11import type {AnyParseNode} from "./parseNode";12import type {MacroMap} from "./defineMacro";13 14export type StrictFunction =15 (errorCode: string, errorMsg: string, token?: Token | AnyParseNode) =>16 (boolean | string) | null | undefined;17 18export type TrustContextTypes = {19 "\\href": {20 command: "\\href";21 url: string;22 protocol?: string;23 };24 "\\includegraphics": {25 command: "\\includegraphics";26 url: string;27 protocol?: string;28 };29 "\\url": {30 command: "\\url";31 url: string;32 protocol?: string;33 };34 "\\htmlClass": {35 command: "\\htmlClass";36 class: string;37 };38 "\\htmlId": {39 command: "\\htmlId";40 id: string;41 };42 "\\htmlStyle": {43 command: "\\htmlStyle";44 style: string;45 };46 "\\htmlData": {47 command: "\\htmlData";48 attributes: Record<string, string>;49 };50};51export type AnyTrustContext = TrustContextTypes[keyof TrustContextTypes];52export type TrustFunction = (context: AnyTrustContext) => boolean | null | undefined;53export type SettingsOptions = Partial<Settings>;54 55type EnumType<T extends string = string> = {56 enum: T[];57};58 59type Type = "boolean" | "string" | "number" | "object" | "function" | EnumType;60/**61 * Union of all values that appear as schema defaults, cliDefaults, or62 * cliProcessor return values. StrictFunction / TrustFunction are63 * option-value types, not default/schema values, so they are excluded.64 */65type SettingsValue = boolean | string | number | MacroMap | string[];66type DefaultValue = Exclude<SettingsValue, string[]>;67 68type SchemaMetadata<K extends keyof SettingsOptions> = {69 /**70 * The description.71 */72 description?: string;73 /**74 * The function to process the option.75 */76 processor?: (value: Settings[K]) => Settings[K];77 /**78 * The command line argument. See Commander.js docs for more information.79 * If not specified, the name prefixed with -- will be used. Set false not80 * to add to the CLI.81 */82 cli?: string | false;83 /**84 * The default value for the CLI.85 */86 cliDefault?: SettingsValue;87 /**88 * The description for the CLI. If not specified, the description for the89 * option will be used.90 */91 cliDescription?: string;92 /**93 * The custom argument processor for the CLI. See Commander.js docs for94 * more information. Signature varies per setting (e.g. parseFloat,95 * or (def, defs) => defs.push(def) for macros).96 */97 cliProcessor?: (...args: any[]) => SettingsValue;98};99 100type BooleanSchema<K extends keyof SettingsOptions> = SchemaMetadata<K> & {101 /**102 * Allowed type(s) of the value.103 */104 type: "boolean";105 /**106 * The default value. If not specified, false will be used.107 */108 default?: Extract<Settings[K], boolean>;109};110 111type StringSchema<K extends keyof SettingsOptions> = SchemaMetadata<K> & {112 /**113 * Allowed type(s) of the value.114 */115 type: "string";116 /**117 * The default value. If not specified, an empty string will be used.118 */119 default?: Extract<Settings[K], string>;120};121 122type NumberSchema<K extends keyof SettingsOptions> = SchemaMetadata<K> & {123 /**124 * Allowed type(s) of the value.125 */126 type: "number";127 /**128 * The default value. If not specified, 0 will be used.129 */130 default?: Extract<Settings[K], number>;131};132 133type ObjectSchema<K extends keyof SettingsOptions> = SchemaMetadata<K> & {134 /**135 * Allowed type(s) of the value.136 */137 type: "object";138 /**139 * The default value. If not specified, an empty object will be used.140 */141 default?: Extract<Settings[K], MacroMap>;142};143 144type FunctionSchema<K extends keyof SettingsOptions> = SchemaMetadata<K> & {145 /**146 * Allowed type(s) of the value.147 */148 type: "function";149 /**150 * Settings do not currently use function-valued defaults.151 */152 default?: never;153};154 155type EnumSchema<K extends keyof SettingsOptions> = SchemaMetadata<K> & {156 /**157 * Allowed type(s) of the value.158 */159 type: EnumType<Extract<Settings[K], string>>;160 /**161 * The default value. If not specified, the first enum value will be used.162 */163 default?: Extract<Settings[K], string>;164};165 166type SingleTypeSchema<K extends keyof SettingsOptions> =167 | BooleanSchema<K>168 | StringSchema<K>169 | NumberSchema<K>170 | ObjectSchema<K>171 | FunctionSchema<K>172 | EnumSchema<K>;173 174type MultiTypeSchema<K extends keyof SettingsOptions> = SchemaMetadata<K> & {175 /**176 * Allowed type(s) of the value.177 */178 type: [SingleTypeSchema<K>["type"], ...Array<SingleTypeSchema<K>["type"]>];179 /**180 * The default value. If not specified, the first allowed type determines181 * the default value.182 */183 default?: Extract<Settings[K], DefaultValue>;184};185 186type SchemaEntry<K extends keyof SettingsOptions> =187 | SingleTypeSchema<K>188 | MultiTypeSchema<K>;189 190type Schema = {191 [key in keyof SettingsOptions]?: SchemaEntry<key>;192};193 194// TODO: automatically generate documentation195// TODO: check all properties on Settings exist196// TODO: check the type of a property on Settings matches197export const SETTINGS_SCHEMA: Schema = {198 displayMode: {199 type: "boolean",200 description: "Render math in display mode, which puts the math in " +201 "display style (so \\int and \\sum are large, for example), and " +202 "centers the math on the page on its own line.",203 cli: "-d, --display-mode",204 },205 output: {206 type: {enum: ["htmlAndMathml", "html", "mathml"]},207 description: "Determines the markup language of the output.",208 cli: "-F, --format <type>",209 },210 leqno: {211 type: "boolean",212 description: "Render display math in leqno style (left-justified tags).",213 },214 fleqn: {215 type: "boolean",216 description: "Render display math flush left.",217 },218 throwOnError: {219 type: "boolean",220 default: true,221 cli: "-t, --no-throw-on-error",222 cliDescription: "Render errors (in the color given by --error-color) ins" +223 "tead of throwing a ParseError exception when encountering an error.",224 },225 errorColor: {226 type: "string",227 default: "#cc0000",228 cli: "-c, --error-color <color>",229 cliDescription: "A color string given in the format 'rgb' or 'rrggbb' " +230 "(no #). This option determines the color of errors rendered by the " +231 "-t option.",232 cliProcessor: (color) => "#" + color,233 },234 macros: {235 type: "object",236 cli: "-m, --macro <def>",237 cliDescription: "Define custom macro of the form '\\foo:expansion' (use " +238 "multiple -m arguments for multiple macros).",239 cliDefault: [],240 cliProcessor: (def, defs) => {241 defs.push(def);242 return defs;243 },244 },245 minRuleThickness: {246 type: "number",247 description: "Specifies a minimum thickness, in ems, for fraction lines," +248 " `\\sqrt` top lines, `{array}` vertical lines, `\\hline`, " +249 "`\\hdashline`, `\\underline`, `\\overline`, and the borders of " +250 "`\\fbox`, `\\boxed`, and `\\fcolorbox`.",251 processor: (t) => Math.max(0, t),252 cli: "--min-rule-thickness <size>",253 cliProcessor: parseFloat,254 },255 colorIsTextColor: {256 type: "boolean",257 description: "Makes \\color behave like LaTeX's 2-argument \\textcolor, " +258 "instead of LaTeX's one-argument \\color mode change.",259 cli: "-b, --color-is-text-color",260 },261 strict: {262 type: [{enum: ["warn", "ignore", "error"]}, "boolean", "function"],263 description: "Turn on strict / LaTeX faithfulness mode, which throws an " +264 "error if the input uses features that are not supported by LaTeX.",265 cli: "-S, --strict",266 cliDefault: false,267 },268 trust: {269 type: ["boolean", "function"],270 description: "Trust the input, enabling all HTML features such as \\url.",271 cli: "-T, --trust",272 },273 maxSize: {274 type: "number",275 default: Infinity,276 description: "If non-zero, all user-specified sizes, e.g. in " +277 "\\rule{500em}{500em}, will be capped to maxSize ems. Otherwise, " +278 "elements and spaces can be arbitrarily large",279 processor: (s) => Math.max(0, s),280 cli: "-s, --max-size <n>",281 cliProcessor: parseInt,282 },283 maxExpand: {284 type: "number",285 default: 1000,286 description: "Limit the number of macro expansions to the specified " +287 "number, to prevent e.g. infinite macro loops. If set to Infinity, " +288 "the macro expander will try to fully expand as in LaTeX.",289 processor: (n) => Math.max(0, n),290 cli: "-e, --max-expand <n>",291 cliProcessor: (n) => (n === "Infinity" ? Infinity : parseInt(n)),292 },293 globalGroup: {294 type: "boolean",295 cli: false,296 },297};298 299function getImplicitDefault(type: "boolean"): boolean;300function getImplicitDefault(type: "string"): string;301function getImplicitDefault(type: "number"): number;302function getImplicitDefault(type: "object"): MacroMap;303function getImplicitDefault(type: "function"): never;304function getImplicitDefault<T extends string>(type: EnumType<T>): T;305function getImplicitDefault(type: Type): DefaultValue;306function getImplicitDefault(type: Type): DefaultValue {307 if (typeof type !== 'string') {308 return type.enum[0];309 }310 switch (type) {311 case 'boolean':312 return false;313 case 'string':314 return '';315 case 'number':316 return 0;317 case 'object':318 return {};319 default:320 throw new Error(321 "Unexpected schema type; settings must declare an explicit default.");322 }323}324 325function getDefaultValue<K extends keyof SettingsOptions>(326 schema: SchemaEntry<K>,327): Settings[K];328function getDefaultValue(schema: SchemaEntry<keyof SettingsOptions>): DefaultValue {329 if (schema.default !== undefined) {330 return schema.default;331 }332 const type = Array.isArray(schema.type) ? schema.type[0] : schema.type;333 return getImplicitDefault(type);334}335 336function applySetting<K extends keyof SettingsOptions>(337 target: Settings,338 prop: K,339 options: SettingsOptions,340 schema: SchemaEntry<K>,341) {342 const optionValue = options[prop];343 target[prop] = optionValue !== undefined344 ? (schema.processor345 ? schema.processor(optionValue)346 : optionValue)347 : getDefaultValue(schema);348}349 350/**351 * The main Settings object352 *353 * The current options stored are:354 * - displayMode: Whether the expression should be typeset as inline math355 * (false, the default), meaning that the math starts in356 * \textstyle and is placed in an inline-block); or as display357 * math (true), meaning that the math starts in \displaystyle358 * and is placed in a block with vertical margin.359 */360export default class Settings {361 displayMode!: boolean;362 output!: "html" | "mathml" | "htmlAndMathml";363 leqno!: boolean;364 fleqn!: boolean;365 throwOnError!: boolean;366 errorColor!: string;367 macros!: MacroMap;368 minRuleThickness!: number;369 colorIsTextColor!: boolean;370 strict!: boolean | "ignore" | "warn" | "error" | StrictFunction;371 trust!: boolean | TrustFunction;372 maxSize!: number;373 maxExpand!: number;374 globalGroup!: boolean;375 376 constructor(options: SettingsOptions = {}) {377 // allow null options378 options = options || {};379 for (const prop of Object.keys(SETTINGS_SCHEMA) as Array<keyof SettingsOptions>) {380 const schema = SETTINGS_SCHEMA[prop] as SchemaEntry<typeof prop> | undefined;381 if (schema) {382 // TODO: validate options383 applySetting(this, prop, options, schema);384 }385 }386 }387 388 /**389 * Report nonstrict (non-LaTeX-compatible) input.390 * Can safely not be called if `this.strict` is false in JavaScript.391 */392 reportNonstrict(errorCode: string, errorMsg: string,393 token?: Token | AnyParseNode) {394 let strict: Settings["strict"] | ReturnType<StrictFunction> = this.strict;395 if (typeof strict === "function") {396 // Allow return value of strict function to be boolean or string397 // (or null/undefined, meaning no further processing).398 strict = strict(errorCode, errorMsg, token);399 }400 if (!strict || strict === "ignore") {401 return;402 } else if (strict === true || strict === "error") {403 throw new ParseError(404 "LaTeX-incompatible input and strict mode is set to 'error': " +405 `${errorMsg} [${errorCode}]`, token);406 } else if (strict === "warn") {407 typeof console !== "undefined" && console.warn(408 "LaTeX-incompatible input and strict mode is set to 'warn': " +409 `${errorMsg} [${errorCode}]`);410 } else { // won't happen in type-safe code411 typeof console !== "undefined" && console.warn(412 "LaTeX-incompatible input and strict mode is set to " +413 `unrecognized '${strict}': ${errorMsg} [${errorCode}]`);414 }415 }416 417 /**418 * Check whether to apply strict (LaTeX-adhering) behavior for unusual419 * input (like `\\`). Unlike `nonstrict`, will not throw an error;420 * instead, "error" translates to a return value of `true`, while "ignore"421 * translates to a return value of `false`. May still print a warning:422 * "warn" prints a warning and returns `false`.423 * This is for the second category of `errorCode`s listed in the README.424 */425 useStrictBehavior(errorCode: string, errorMsg: string,426 token?: Token | AnyParseNode): boolean {427 let strict: Settings["strict"] | ReturnType<StrictFunction> = this.strict;428 if (typeof strict === "function") {429 // Allow return value of strict function to be boolean or string430 // (or null/undefined, meaning no further processing).431 // But catch any exceptions thrown by function, treating them432 // like "error".433 try {434 strict = strict(errorCode, errorMsg, token);435 } catch (error) {436 strict = "error";437 }438 }439 if (!strict || strict === "ignore") {440 return false;441 } else if (strict === true || strict === "error") {442 return true;443 } else if (strict === "warn") {444 typeof console !== "undefined" && console.warn(445 "LaTeX-incompatible input and strict mode is set to 'warn': " +446 `${errorMsg} [${errorCode}]`);447 return false;448 } else { // won't happen in type-safe code449 typeof console !== "undefined" && console.warn(450 "LaTeX-incompatible input and strict mode is set to " +451 `unrecognized '${strict}': ${errorMsg} [${errorCode}]`);452 return false;453 }454 }455 456 /**457 * Check whether to test potentially dangerous input, and return458 * `true` (trusted) or `false` (untrusted). The sole argument `context`459 * should be an object with `command` field specifying the relevant LaTeX460 * command (as a string starting with `\`), and any other arguments, etc.461 * If `context` has a `url` field, a `protocol` field will automatically462 * get added by this function (changing the specified object).463 */464 isTrusted(context: AnyTrustContext): boolean {465 if ("url" in context && context.url && !context.protocol) {466 const protocol = protocolFromUrl(context.url);467 if (protocol == null) {468 return false;469 }470 context.protocol = protocol;471 }472 const trust = typeof this.trust === "function"473 ? this.trust(context)474 : this.trust;475 return Boolean(trust);476 }477}478 